2017-07-29 10 views
1

ファイルからユーザー名とパスワードの組み合わせを取得して書き込むスクリプトをPython 3.5.3で作成しています別のファイル。このスクリプトは、Windows 10搭載のマシン上で書かれていました。しかし、私がYosemiteを実行しているMacBookでスクリプトを実行しようとすると、ASCIIエンコーディングと何らかの関係があるエラーが発生しました。UnicodeDecodeError: 'ascii'コーデックは、位置2370のバイト0xaaをデコードできません:序数が範囲内にありません(128)

関連の機能はこれです:

def buildDatabase(): 
     print("Building database, this may take some time...") 
     passwords = open("10-million-combos.txt", "r") #File with user/pword combos. 
     hashWords = open("Hashed Combos.txt", "a") #File where user/SHA-256 encrypted pwords will be stored. 
     j = 0 
     hashTable = [[ None ] for x in range(60001)] #A hashtable with 30,000 elements, quadratic probing means size must = 2 x the final size + 1 
     for line in passwords: 
       toSearch = line 
       i = q = toSearch.find("\t") #The username/pword combos are formatted: username\tpassword\n. 
       n = toSearch.find("\n") 
       password = line[i:n-1] #i is the start of the password, n is the end of it 
       username = toSearch[ :q] + ":" #q is the end of the username 
       byteWord = password.encode('UTF-8') 
       sha.update(byteWord) 
       toWrite = sha.hexdigest() #password is encrypted to UTF-8, run thru SHA-256, and stored in toWrite 
       skip = False 
       if len(password) == 0: #if le(password) is 0, just skip it 
         skip = True 
       if len(password) == 1: 
         doModulo = ord(password[0]) ** 4 
       if len(password) == 2: 
         doModulo = ord(password[0]) * ord(password[0]) * ord(password[1]) * ord(password[1]) 
       if len(password) == 3: 
         doModulo = ord(password[0]) * ord(password[0]) * ord(password[1]) * ord(password[2]) 
       if len(password) > 3: 
         doModulo = ord(password[0]) * ord(password[1]) * ord(password[2]) * ord(password[3]) 
       assignment = doModulo % 60001 
       #The if block above gives each combo an assignment number for a hash table, indexed by password because they're more unique than usernames 
       successful = False 
       collision = 0 

次のようにエラーがある:

Traceback (most recent call last): 
    File "/Users/connerboehm/Documents/Conner B/PythonFinalProject.py", line 104, in <module> 
    buildDatabase() 
    File "/Users/connerboehm/Documents/Conner B/PythonFinalProject.py", line 12, in buildDatabase 
    for line in passwords: 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode 
    return codecs.ascii_decode(input, self.errors)[0] 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xaa in position 2370: ordinal not in range(128) 

ここで何が起こっていますか?以前はこのエラーが発生していませんでしたが、UTF-8にエンコードしようとすると問題はありません。

編集:メモ帳はANSIでエンコードされます。 UTF-8にエンコーディングを変更するだけで(データをコピーして新しい.txtファイルに貼り付ける)、問題は解決しました。

+0

ASCIIやUnicodeなどの文字エンコーディングを学習するには、UTF-8を開始するのがよいでしょう。 – zaph

答えて

2

あなたのプログラムは、ファイル"10-million-combos.txt"で使用されているコーデックを示していないので、Pythonはこの場合、ASCIIとしてデコードしようとしています。 0xaaはASCII序数ではないため、失敗しました。ファイルに使用されているコーデックを特定し、encodingパラメータのopenに渡します。

+0

"あなたのファイルで使用されているコーデックを特定する"は、実行したよりも簡単です。おそらく、あなたは[chardet](https://pypi.python.org/pypi/chardet)モジュールのように、それを行う方法を提案できますか? –

関連する問題