How come string.maketrans does not work in Python 3.1?
I'm a Python newbie.
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
What does "must be bytes objects" mean? Could anyone pleas开发者_运维百科e help post a working code for Python 3.1 if it's possible?
You don't need to use bytes.maketrans()
when str
would be simpler and eliminate the need for the 'b' prefix:
print("Swap vowels for numbers.".translate(str.maketrans('aeiou', '12345')))
Stop trying to learn Python 3 by reading Python 2 documentation.
intab = 'aeiou'
outtab = '12345'
s = 'this is string example....wow!!!'
print(s.translate({ord(x): y for (x, y) in zip(intab, outtab)}))
Strings are not bytes.
This is a simple definition in Python 3.
Strings are Unicode (which are not bytes) Unicode strings use "..."
or '...'
Bytes are bytes (which are not strings) Byte strings use b"..."
or b'...'
.
Use b"aeiou"
to create a byte sequence composed of the ASCII codes for certain letters.
In Python 3, the string.maketrans()
function is deprecated and is replaced by new static methods, bytes.maketrans()
and 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.
"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.
If you absolutely insist on working with 8-bit bytes:
>>> 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!!!'
>>>
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() M2th4dsHere is an example of performing translation and deletion, in Python 2 or 3:
import sys
DELETE_CHARS = '!@#$%^*()+=?\'\"{}[]<>!`:;|\\/-,.'
if sys.version_info < (3,):
import string
def fix_string(s, old=None, new=None):
if old:
table = string.maketrans(old, new)
else:
table = None
return s.translate(table, DELETE_CHARS)
else:
def fix_string(s, old='', new=''):
table = s.maketrans(old, new, DELETE_CHARS)
return s.translate(table)
maketrans is a string function
use below logic to use translate using maketrans
print('maketrans' , '& translate')
intab = "aeiou"
outtab = "12345"
str = "Fruits are delicious and healthy!!!"
trantab = str.maketrans(intab, outtab)
print (str.translate(trantab))
精彩评论