0

I am getting some error while escaping some special characters from string using Python. The error is given below.

Error:

trans_table = string.maketrans(trans_dict)
TypeError: maketrans() takes exactly 2 arguments (1 given)

Code:

import sys
import string
if "win" in sys.platform:
    special = """( ) < >  * ‘  = ? ; [ ] ^ ~ ! . ” % @ / \ : + , `""".split()
else:
    special = """{ }  ( ) < >  * ‘  = ? ; [ ]  $ – # ~ ! . ” %  / \ : + , `""".split()

trans_dict = {character: None for character in special}
trans_table = string.maketrans(trans_dict)
print("Lo+=r?e~~m ipsum dol;or sit!! amet, consectet..ur ad%".translate(trans_table))

Here I need to escape some special characters from the string.

4
  • You are using maketrans incorrectly. See this Commented Jul 17, 2017 at 8:53
  • I am using python 2.7 and can you please make this correct as per my need ? Commented Jul 17, 2017 at 8:55
  • Can you add the desired output to the question? Commented Jul 17, 2017 at 8:56
  • @Ev.Kounis : The output should be Lorem ipsum dolor sit amet consectetur ad . Here I need to escape the given special characters from the string. Commented Jul 17, 2017 at 8:58

2 Answers 2

2

maketrans does not take a dict in Python 2, it only takes two string parameters with equal lengths.

To delete characters you should pass a deletechars parameter to translate with a translation mapping empty strings:

...
delete_chars = ''.join(special)
trans_table = string.maketrans('', '')
print("Lo+=r?e~~m ipsum dol;or sit!! amet, consectet..ur ad%"
       .translate(trans_table, delete_chars))
# Lorem ipsum dolor sit amet consectetur ad

You could also drop the translation table and pass None to translate:

print("Lo+=r?e~~m ipsum dol;or sit!! amet, consectet..ur ad%"
  .translate(None, delete_chars))
Sign up to request clarification or add additional context in comments.

4 Comments

How is this possible? Does this not go against the documentation?
@Ev.Kounis What documentation are you reading? Python 2?
Thanks Moses. I need some coffee! It seems you dont have to set up the table if no replacement has to be done. So, .translate(None, delete_chars) works too
@Ev.Kounis Ah yes, didn't see the None part. Thanks for the pointer.
1

Python 3

import sys
if "win" in sys.platform:
    special = """()<>*‘=?;[]^~!.”%@/\:+,`"""
else:
    special = """{}()<>*‘=?;[]$–#~!.”%/\:+,`"""

translator = str.maketrans('', '', special)
print("Lo+=r?e~~m ipsum dol;or sit!! amet, consectet..ur ad%".translate(translator))
# Lorem ipsum dolor sit amet consectetur ad

For more information on how the new maketrans works see this


Python 2

The code given above does not work with Python 2. For a working solution that uses .translate see the answer by @Moses. I would do it like that:

import sys
if "win" in sys.platform:
    special = """()<>*‘=?;[]^~!.”%@/\:+,`"""
else:
    special = """{}()<>*‘=?;[]$–#~!.”%/\:+,`"""

my_string = "Lo+=r?e~~m ipsum dol;or sit!! amet, consectet..ur ad%"
my_string = ''.join(x for x in my_string if x not in special)
# Lorem ipsum dolor sit amet consectetur ad

3 Comments

As I am using python 2.7, str might cause error.
True, missed that.
I did as per you but still this TypeError: maketrans() takes exactly 2 arguments (3 given) error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.