This question already has an answer here:
how to interpret this error “UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 164: ordinal not in range(128)”
(1个答案)
2年前关闭。
我正在用Python 3.5.3编写脚本,该脚本从文件中获取用户名/密码组合,并将其写入另一个文件。该脚本是在装有Windows 10的计算机上编写的,并且可以运行。但是,当我尝试在运行Yosemite的MacBook上运行脚本时,出现了与ASCII编码有关的错误。
相关功能是这样的:
错误如下:
这里发生了什么事?我以前在Windows上还没有收到此错误,并且尝试将其编码为UTF-8也看不到任何问题。
编辑:记事本在ANSI中编码。将编码更改为UTF-8(仅将数据复制并粘贴到新的.txt文件中)即可解决此问题。
(1个答案)
2年前关闭。
我正在用Python 3.5.3编写脚本,该脚本从文件中获取用户名/密码组合,并将其写入另一个文件。该脚本是在装有Windows 10的计算机上编写的,并且可以运行。但是,当我尝试在运行Yosemite的MacBook上运行脚本时,出现了与ASCII编码有关的错误。
相关功能是这样的:
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)
这里发生了什么事?我以前在Windows上还没有收到此错误,并且尝试将其编码为UTF-8也看不到任何问题。
编辑:记事本在ANSI中编码。将编码更改为UTF-8(仅将数据复制并粘贴到新的.txt文件中)即可解决此问题。
最佳答案
您的程序没有说明文件"10-million-combos.txt"
中使用了哪种编解码器,因此Python在这种情况下尝试将其解码为ASCII。 0xaa不是ASCII序数,因此失败。确定文件中使用了哪种编解码器,并将其传递给open的encoding
参数。