我在使用Python转义字符串转义某些特殊字符时出现了一些错误。错误在下面给出。使用Python从字符串转义特殊字符时得到TypeError
错误:
trans_table = string.maketrans(trans_dict)
TypeError: maketrans() takes exactly 2 arguments (1 given)
我解释下面我的代码。
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))
这里我需要从字符串中转义一些特殊字符。
你是错误地使用'maketrans'。看到[这](https://stackoverflow.com/questions/41535571/how-to-explain-the-str-maketrans-function-in-python-3-6) –
我使用'python 2.7',你能请根据我的需要做出正确的答案? – satya
你可以在问题中添加所需的输出吗? –