我收到試圖運行在Python 3.5.2外殼命令時,下面的錯誤:的Python AttributeError的:模塊「串」有沒有屬性「maketrans」
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit
(Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
>>> folder = 'C:/users/kdotz/desktop'
>>> f = open(folder + '/genesis.txt', 'r')
>>> import operator, time, string
>>> start=time.time()
>>> genesis = {}
>>> for line in f:
line=line.split()
for word in line:
word = word.lower()
new_word=word.translate(string.maketrans("",""), string.punctutation)
if new_word in genesis:
genesis[new_word]+=1
else:
genesis[new_word]=1
Traceback (most recent call last):
File "<pyshell#15>", line 5, in <module>
new_word=word.translate(string.maketrans("",""), string.punctutation)
AttributeError: module 'string' has no attribute 'maketrans'
我在做什麼錯誤?我在代碼的頂部導入字符串。先謝謝您的幫助!
在Python 3中'maketrans'是'str'的一種方法。 – vaultah