为什么 string.maketrans 在 Python 3.1 中不起作用? [英] How come string.maketrans does not work in Python 3.1?
问题描述
我是 Python 新手.
I'm a Python newbie.
为什么这个在 Python 3.1 中不起作用?
How come this doesn't work in 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);
当我执行上述代码时,我得到以下信息:
When I executed the above code, I get the following instead:
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
必须是字节对象"是什么意思?如果可能的话,有人可以帮忙发布 Python 3.1 的工作代码吗?
What does "must be bytes objects" mean? Could anyone please help post a working code for Python 3.1 if it's possible?
推荐答案
字符串不是字节.
这是 Python 3 中的一个简单定义.
This is a simple definition in Python 3.
字符串是 Unicode(不是字节)Unicode 字符串使用 "..."
或 '...'
Strings are Unicode (which are not bytes) Unicode strings use "..."
or '...'
字节是字节(不是字符串)字节字符串使用 b"..."
或 b'...'
.
Bytes are bytes (which are not strings) Byte strings use b"..."
or b'...'
.
使用 b"aeiou"
来创建由某些字母的 ASCII 码组成的字节序列.
Use b"aeiou"
to create a byte sequence composed of the ASCII codes for certain letters.
这篇关于为什么 string.maketrans 在 Python 3.1 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!