我有一个字符串,我正在使用zlib
进行压缩,将其存储在字典中并创建字典的md5
哈希值。但是我收到了错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 1: invalid start byte
代码是:
data['extras'] = zlib.compress("My string".encode("utf-8")) //The string is very large that\'s why it\'s needed to be compressed to save up memory
checkup['hash'] = hashlib.md5(json.dumps(dict(data), sort_keys=True)).hexdigest()
字典就像:
{'extras':'x\x9cK\x04\x00\x00b\x00b'}
有谁能告诉我如何在JSON中转储这个字典/字符串?
字符串是一个长json。类似的东西:
{
"listing": {
"policies": null,
"policy_explanation": "Some Text",
"policy_name": "Flexi3",
"updated": "7 weeks ago",
"city": "Bengaluru",
"country": "India",
.
.
.
}
答案 0 :(得分:5)
您可以先对其进行base64编码,以使其正常工作。它会为字符串添加一些大小,但可能比通过先压缩它保存的小一些:
data['extras'] = base64.b64encode(zlib.compress("My string".encode("utf-8")))