2016-11-23 16 views
1

私は、Python 3.5.2シェルのコマンドを実行しようとすると、以下のエラー受け付けております:PythonのはAttributeError:モジュール「string」は持っていない属性「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' 

私が間違ってやっている何を?私はコードの先頭に文字列をインポートします。助けを前にありがとう!あなたが見ることができるように

>>> import string 
>>> 
>>> dir(string) 
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace'] 
>>> 

、何があります:

+1

Python 3では 'maketrans'は' str'のメソッドです。私は今取得 – vaultah

答えて

1

maketransは、あなたがこの種の問題を持っている時はいつでもことを確認しないようにdir()を使用することができ、新たな静的メソッドの賛成で

The string.maketrans() function is deprecated and is replaced by new static methods, bytes.maketrans() and bytearray.maketrans(). This change solves the confusion around which types were supported by the string module. Now, str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.

を推奨されていません上記の結果リストにmaketrans

+0

エラーがあります。fにおけるラインの '>>>: \tライン= line.split()行の単語の \t: \t \t単語= word.lower() \t \t new_word = word.translate起源で(str.maketrans( ""、 "")、string.punctuation) \t \t new_word場合: 他\t \t \tジェネシス[new_word] + = 1 \t \t: \t \t \tジェネシス[new_word] = '1' \t \t \t 'トレースバック(最新の呼び出しの最後): new_word = word.translate(str.maketrans( ""、 "" で ファイル ""、5行目、 )、string.punctuation) TypeError:translate()はちょうど1つの引数をとります。 ' –

+0

'string.punctuation'を削除する必要があります – ettanany

+1

' str.translate() 'ドキュメントはhttps://docs.pythonでチェックしてください。 org/3/library/stdtypes.html#str.translate – ettanany

関連する問題