Python TypeError:translate()只接受一个参数(给定2个)

Python TypeError:translate()只接受一个参数(给定2个),python,string,python-2.7,python-3.x,Python,String,Python 2.7,Python 3.x,我的代码在Python2.x版本上运行良好,但当我试图在Python3.x版本上运行它时,它给出了错误 主题:需要缩写短信编码中的任何消息 代码: 输出: 2.x版本: I will not repeat mistakes I wll nt rpt mstks I will not repeat mistakes Traceback (most recent call last): File "python", line 18, in <module> File "pyth

我的代码在Python2.x版本上运行良好,但当我试图在Python3.x版本上运行它时,它给出了错误

主题:需要缩写短信编码中的任何消息

代码:

输出:

2.x版本:

I will not repeat mistakes
I wll nt rpt mstks
I will not repeat mistakes
Traceback (most recent call last):
  File "python", line 18, in <module>
  File "python", line 12, in sms_encoding
TypeError: translate() takes exactly one argument (2 given)
3.x版本:

I will not repeat mistakes
I wll nt rpt mstks
I will not repeat mistakes
Traceback (most recent call last):
  File "python", line 18, in <module>
  File "python", line 12, in sms_encoding
TypeError: translate() takes exactly one argument (2 given)
我不会重复错误
回溯(最近一次呼叫最后一次):
文件“python”,第18行,在
文件“python”,第12行,采用sms_编码
TypeError:translate()只接受一个参数(给定2个)

为什么
translate()
不起作用?有其他解决方法吗?

您需要比较Python 3的
str.translate()
和Python 2的
unicode.translate()
。两者都采用从代码点(整数)到替换(另一个整数或单字符Unicode e字符串)的映射

str
类型有一个静态方法,该方法将要删除的字符(Python 2的
str.translate()
的第二个参数)作为第三个参数,以生成这样的映射。在这里使用:

map = str.maketrans('', '', 'aeiouAEIOU')
a = data_list[i].translate(map)
这将输出一个字典,将每个元音代码点映射到
None

>>> str.maketrans('', '', 'aeiouAEIOU')
{97: None, 101: None, 105: None, 111: None, 117: None, 65: None, 69: None, 73: None, 79: None, 85: None}

抱歉,各位,我找不到存在的问题。谢谢你指出。谢谢你,@Martijn Pieters。