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

[英]How come string.maketrans does not work in Python 3.1?


I'm a Python newbie.

我是一个Python新手。

How come this doesn't work in Python 3.1?

为什么这在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

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.1的工作代码吗?

8 个解决方案

#1


10  

Strings are not bytes.

字符串不是字节。

This is a simple definition in Python 3.

这是Python 3中的一个简单定义。

Strings are Unicode (which are not bytes) Unicode strings use "..." or '...'

字符串是Unicode(不是字节)Unicode字符串使用“...”或“...”

Bytes are bytes (which are not strings) Byte strings use b"..." or b'...'.

字节是字节(不是字符串)字节字符串使用b“...”或b'...'。

Use b"aeiou" to create a byte sequence composed of the ASCII codes for certain letters.

使用b“aeiou”创建由某些字母的ASCII代码组成的字节序列。

#2


31  

You don't need to use bytes.maketrans() when str would be simpler and eliminate the need for the 'b' prefix:

当str更简单并且不需要'b'前缀时,你不需要使用bytes.maketrans():

print("Swap vowels for numbers.".translate(str.maketrans('aeiou', '12345')))

#3


18  

Stop trying to learn Python 3 by reading Python 2 documentation.

通过阅读Python 2文档停止尝试学习Python 3。

intab = 'aeiou'
outtab = '12345'

s = 'this is string example....wow!!!'

print(s.translate({ord(x): y for (x, y) in zip(intab, outtab)}))

#4


5  

In Python 3, the string.maketrans() function is deprecated and is replaced by new static methods, bytes.maketrans() and bytearray.maketrans().

在Python 3中,不推荐使用string.maketrans()函数,并将其替换为新的静态方法bytes.maketrans()和bytearray.maketrans()。

This change solves the confusion around which types were supported by the string module.

此更改解决了字符串模块支持哪些类型的混淆。

Now str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.

现在str,bytes和bytearray都有自己的maketrans和translate方法,并带有适当类型的中间转换表。

#5


3  

"this is string example....wow!!!".translate(str.maketrans("aeiou","12345"))

This works, and no additional byte transformation. I don't know the reason why to use byte instead of str.

这工作,没有额外的字节转换。我不知道为什么要使用byte而不是str。

#6


0  

If you absolutely insist on working with 8-bit bytes:

如果你绝对坚持使用8位字节:

>>> intab = b"aeiou"
>>> outtab = b"12345"
>>> trantab = bytes.maketrans(intab, outtab)
>>> strg = b"this is string example....wow!!!";
>>> print(strg.translate(trantab));
b'th3s 3s str3ng 2x1mpl2....w4w!!!'
>>>

#7


0  

Hey here is the simple one liner which worked perfectly for me

嘿,这是一个简单的衬垫,对我来说非常合适

import string
a = "Learning Tranlate() Methods"
print (a.translate(bytes.maketrans(b"aeiou", b"12345")))*
OUTPUT :::: L21rn3ng Tr1nl1t2() M2th4ds

#8


-1  

Here's my final Python (3.1) code posted here just for reference:

这是我最后发布的Python(3.1)代码,仅供参考:

"this is string example....wow!!!".translate(bytes.maketrans(b"aeiou",b"12345"))

Short and sweet, love it.

简短而甜蜜,喜欢它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/06/13/2fac54eb183e0e880fc42949f7ce6050.html



 
  © 2014-2022 ITdaan.com 联系我们: