【发布时间】:2016-02-02 05:35:09
【问题描述】:
为了学习 argparse 模块,我从官方文档站点复制了一些代码。
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print (args.accumulate(args.integers))
但是当我输入时
python argparseTesting.py
在命令行中,则出现以下错误:
test
case
with
strings
0
1
2
3
0
1
2
3
At Index 0 of our array, we have pens
At Index 1 of our array, we have staplers
At Index 2 of our array, we have flame-throwers
At Index 3 of our array, we have binders
Traceback (most recent call last):
File "optparser_short.py", line 1, in <module>
import argparse
File "/usr/local/lib/python2.7/argparse.py", line 90, in <module>
import textwrap as _textwrap
File "/usr/local/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/local/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
在 AttributeError: 'module' object has no attribute 'maketrans' 中,我读到了几乎相同的问题。但是那里的建议没有帮助。我有 python 版本 2.7.11。
希望有人能帮忙。
最好的问候,
【问题讨论】:
-
您有一个名为
string.py的本地文件。重命名它。 -
重命名,好的,但是改成什么?制作trans.py ?
-
没关系,只要不是和你要使用的内置模块同名即可。
-
我改名了,还是一样的错误