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.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • You are using `maketrans` incorrectly. See [this](https://stackoverflow.com/questions/41535571/how-to-explain-the-str-maketrans-function-in-python-3-6) – Ma0 Jul 17 '17 at 08:53
  • I am using `python 2.7` and can you please make this correct as per my need ? – satya Jul 17 '17 at 08:55
  • Can you add the desired output to the question? – Ma0 Jul 17 '17 at 08: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. – satya Jul 17 '17 at 08:58

2 Answers2

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))
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • How is this possible? Does this not go against the documentation? – Ma0 Jul 17 '17 at 09:07
  • @Ev.Kounis What documentation are you reading? [Python 2](https://docs.python.org/2/library/string.html#string.translate)? – Moses Koledoye Jul 17 '17 at 09:08
  • 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 – Ma0 Jul 17 '17 at 09:10
  • @Ev.Kounis Ah yes, didn't see the `None` part. Thanks for the pointer. – Moses Koledoye Jul 17 '17 at 09:11
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
Ma0
  • 15,057
  • 4
  • 35
  • 65