相关疑难解决方法(0)

为什么string.maketrans在Python 3.1中不起作用?

我是一个Python新手.

为什么在Python 3.1中不起作用?

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab);
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码时,我得到以下代码:

Traceback (most recent call last):
  File "<pyshell#119>", line 1, in <module>
    transtab = maketrans(intab, outtab)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/string.py", line 60, in maketrans
    raise TypeError("maketrans arguments must be bytes objects")
TypeError: maketrans arguments must be bytes objects
Run Code Online (Sandbox Code Playgroud)

"必须是字节对象"是什么意思?如果有可能,有人可以帮助发布Python 3.1的工作代码吗?

python python-3.x

18
推荐指数
5
解决办法
5万
查看次数

标签 统计

python ×1

python-3.x ×1