Nav*_*777 0 python unicode dictionary python-unicode
我在 python 中有一个字典,其中包含 unicode 值。我想计算这本字典的 md5 总和。我尝试使用这个问题的答案:Computing an md5 hash of a data structure
import hashlib
import bencode
data = {'unicode_text': '????'}
data_md5 = hashlib.md5(bencode.bencode(data)).hexdigest()
print data_md5
Run Code Online (Sandbox Code Playgroud)
但问题是bencode
返回此错误:
KeyError: <type 'unicode'>
Run Code Online (Sandbox Code Playgroud)
该bencode
库似乎不支持 unicode 对象(无论如何,它是为 Python 2 编写的,我猜您正在使用 Python 3)。使用内置json
模块怎么样?
import hashlib
import json
data = {'unicode_text': '????'}
data_md5 = hashlib.md5(json.dumps(data, sort_keys=True)).hexdigest()
print data_md5
Run Code Online (Sandbox Code Playgroud)