Python 属性错误:'tuple' 对象没有 'encode' 属性 在 hashlib.encode中
我的代码:
for chars in chain(ALC, product(ALC, repeat=2), product(ALC, repeat=3)):
a = hashlib.md5()
a.update(chars.encode('utf-8'))
print(''.join(chars))
print(a.hexdigest())
它返回了这个错误:
Traceback (most recent call last):
File "pyCrack.py", line 18, in <module>
a.update(chars.encode('utf-8'))
AttributeError: 'tuple' object has no attribute 'encode'
完整输出:http://pastebin.com/p1rEcn9H。看起来在尝试继续到“aa”时出现了错误。我该怎么修复这个问题呢?
2 个回答
-1
你的错误是试图把这个元组转换成utf-8格式。可以试着去掉这一行 "a.update(chars.encode('utf-8')"。
当解释器显示 "'tuple' object has no attribute 'encode'" 的时候,这意味着元组这种东西不支持那样的转换。
不过,如果你想要转换所有这些内容,可以在你程序的第一行加上 #coding: utf-8。
5
你正在把不同类型的数据串在一起,这会让人很头疼。
假设ALC
是一个字符串,那么chain
首先会把字符串里的所有字符一个个拿出来。当它接着处理product(ALC, repeat=2)
时,它开始输出tuple
(元组),因为product
就是这么工作的。
只要在你的chain
调用中输出相同类型的数据(也就是说,始终输出元组,当你需要字符串时再把它们合并),这样就不会再头疼了。
for chars in chain(*[product(ALC, repeat=n) for n in range(1,4)]):
...
a.update(''.join(chars).encode('utf-8'))