2017-07-29 6 views
1

Ich schreibe ein Skript in Python 3.5.3, das Benutzername/Passwort Combos aus einer Datei und schreibt sie in eine andere Datei. Das Skript wurde auf einem Rechner mit Windows 10 geschrieben und funktionierte. Als ich jedoch versucht habe, das Skript auf einem MacBook auszuführen, auf dem Yosemite läuft, habe ich einen Fehler bekommen, der etwas mit der ASCII-Codierung zu tun hat.UnicodeDecodeError: 'Ascii' Codec kann Byte 0xaa an Position 2370 nicht dekodieren: Ordinal nicht im Bereich (128)

Die entsprechende Funktion ist dies:

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 

Der Fehler ist wie folgt:

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) 

Was hier passiert? Ich habe diesen Fehler zuvor nicht unter Windows erhalten, und ich sehe kein Problem mit meinem Versuch, in UTF-8 zu kodieren.

Bearbeiten: Editor codiert in ANSI. Durch das Ändern der Codierung (Kopieren und Einfügen der Daten in eine neue TXT-Datei) in UTF-8 wurde das Problem behoben.

+0

Zeit zum Studieren von Zeichenkodierungen wie ASCII und Unicode, UTF-8 ist ein guter Anfang. – zaph

Antwort

2

Ihr Programm sagt nicht, welcher Codec in der Datei "10-million-combos.txt" verwendet wird, also versucht Python in diesem Fall, es als ASCII zu dekodieren. 0xaa ist keine ASCII-Ordinalzahl, daher ist es fehlgeschlagen. Identifizieren Sie, welcher Codec in Ihrer Datei verwendet wird, und geben Sie diesen im Parameter encoding für open weiter.

+0

"Identifizieren Sie, welcher Codec in Ihrer Datei verwendet wird" ist leichter gesagt als getan. Vielleicht könnten Sie einen Weg vorschlagen, wie das Modul [chardet] (https://pypi.python.org/pypi/chardet) funktioniert? –

Verwandte Themen