Soy un novato de Python.¿Cómo es que string.maketrans no funciona en Python 3.1?
¿Cómo es que this no funciona en 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);
Cuando ejecuta el código anterior, me sale el siguiente lugar:
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
¿Qué significa "debe ser bytes objetos" significa? ¿Podría alguien ayudarnos a publicar un código de trabajo para Python 3.1 si es posible?
No funciona, porque string.maketrans doesn' haz lo que piensas Por eso fue desaprobado en 3.1 para bytes.maketrans. Debería usar str.maketrans en lugar de string.maketrans para cadenas y bytes.maketrans para bytes. –
Para ver la diferencia usted mismo, compare la salida de 'print (str.maketrans (" aeiou "," 12345 "))' --que es '{97: 49, 101: 50, 105: 51, 111: 52 , 117: 53} '- a la salida de' print (bytes.maketrans (b "aeiou", b "12345")) '(que es una herramienta larga y fea para mostrar aquí.) – MarkHu