- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 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 编码。将编码(只需将数据复制并粘贴到新的 .txt 文件)更改为 UTF-8 解决了问题。
最佳答案
您的程序没有说明文件“10-million-combos.txt”
中使用的编解码器,因此在本例中Python尝试将其解码为ASCII。 0xaa 不是 ASCII 序数,因此失败。确定文件中使用的编解码器并将其传递到 open 的 encoding
参数中。
关于python - Unicode解码错误: 'ascii' codec can't decode byte 0xaa in position 2370: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45393709/
'aa' 和 '\xaa' 有什么区别? \x 部分是什么意思? Python 文档的哪一章涵盖了这个主题? 最佳答案 前导 \x 转义序列意味着接下来的两个字符被解释为字符代码的十六进制数字,因此
我想使用 UDP 发送内容为 0x11AACC 的数据包,“11AACC”是从数据库中获取的,所以它是字符串。 我不知道怎么把它变成十六进制值11AACC,如果我用[]byte("11AACC")来转
我相信我误解了一些应该很简单的事情。 我正在尝试接受传递给 python 脚本的参数。我期望的参数类型类似于 "\xaa\xbb\xcc\xdd",并且它一直将其转换为二进制(我认为?)而不是实际允许
我尝试使用 pd.read_csv() 函数加载 .csv 文件时遇到错误,尽管文件路径正确且使用原始字符串. import pandas as pd df = pd.read_csv('C:\\U
我是一名优秀的程序员,十分优秀!