【问题标题】:How to calculate MD5 checksum for a unicode dictionary?如何计算 unicode 字典的 MD5 校验和?
【发布时间】:2017-04-07 08:52:06
【问题描述】:

我在 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

但问题是bencode返回这个错误:

KeyError: <type 'unicode'>

【问题讨论】:

    标签: python dictionary unicode python-unicode


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2010-12-12
      • 2011-05-25
      • 2012-05-18
      • 2016-04-02
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多