我使用的是petl软件包,我使用python3.4在virtulaenv中使用pip安装了这个包。当我试图测试petl包是否正确地安装在python shell中时,我这样做是为了检查
$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from petl import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.env/lib/python3.4/site-packages/petl/__init__.py", line 10, in <module>
from petl.util import header, fieldnames, data, records, rowcount, look, see, \
File "/home/user/.env/lib/python3.4/site-packages/petl/util.py", line 14, in <module>
from string import maketrans
ImportError: cannot import name 'maketrans'
>>>我试图检查maketrans是否存在于字符串包中,我运行了以下命令
>>> from string import maketrans
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'maketrans'
>>> 发现默认的python字符串包没有此特性。我不确定为什么petl包没有在其依赖项中提到它而使用它,如果它是默认的python包,那么它为什么会产生导入错误。
不知道发生了什么事,有人能帮忙吗?
发布于 2014-12-18 06:02:41
在Python2中,maketrans是属于string模块的函数。然而,在Python3中,maketrans是str类型的静态方法。
发布于 2015-08-16 18:02:34
因为我在寻找python3.4中它是如何工作的一个清晰的例子,所以我发布了我发现的:
#in py2 you need to "from string import maketrans"
table = "".maketrans('cs', 'kz')
#py2 table = maketrans('cs', 'kz')
len(table)
#in py2 you will get **len(table) = 256 in py3.4 len(table) = 2**
sentence = "cause koala is causing trouble"
sentence.translate(table)发布于 2018-09-02 19:44:24
使用str调用maketrans
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
NUMBERS = '22233344455566677778889999'
## translate a-z char to phone digits
TRANS = str.maketrans(LETTERS, NUMBERS)https://stackoverflow.com/questions/27539884
复制相似问题