為什么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 联系我们: