为什么 string.maketrans 在 Python 3.1 中不起作用? [英] How come string.maketrans does not work in Python 3.1?

查看:34
本文介绍了为什么 string.maketrans 在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆