我尝试运行:
import string
myStr = "This$, (is) my string."
table = string.maketrans( '', '', )
cleanedStr = myStr.translate(table, "(){}<>,:!?_*!$%/.")
print(cleanedStr)
如果我从Sublime Text 2构建它,它对我有用,但如果我在带有python test_script.py
的终端命令行中运行它,我会收到错误:
Traceback (most recent call last):
File "test_script.py", line 6, in <module>
table = string.maketrans( '', '', )
AttributeError: 'module' object has no attribute 'maketrans'
我的Python版本是:
Python 3.4.3 |Anaconda 2.4.1 (x86_64)| (default, Oct 20 2015, 14:27:51)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
我不明白为什么Python从命令行运行时似乎无法做import string
,但是当我从Sublime中调用它时它就可以工作。