简体   繁体   English

Python序列化错误

[英]Python serialization Error

When my python code trying to convert a dictionary object to Json string, it threw the following exception: 当我的python代码试图将字典对象转换为Json字符串时,它抛出以下异常:

SerializationError: ({'status': 'rd', 
'name': 'Detecci\xf3nInt/.unclassified.ez', 'st': 0}, 
UnicodeDecodeError('utf8', 'Detecci\xf3nInt/.unclassified.ez', 7, 8, 
'invalid continuation byte'))

Any hints for fixing this problem please. 请提供解决此问题的任何提示。

By default json.dump() uses UTF8 encoding, however, the value for the name key in your dictionary is not UTF8. 默认情况下, json.dump()使用UTF8编码,但字典中name键的值不是UTF8。 It looks like one of the ISO-8859-X encodings. 它看起来像ISO-8859-X编码之一。 You can specify the encoding with the encoding parameter: 您可以使用encoding参数指定encoding

import json
d = {'status': 'rd', 'name': 'Detecci\xf3nInt/.unclassified.ez', 'st': 0}
s = json.dumps(d, encoding='ISO-8859-1')
print(s)

Output 产量

{"status": "rd", "name": "Detecci\u00f3nInt/.unclassified.ez", "st": 0}

I had a bit of a guess as to which encoding to use, so you might want to check which is the correct encoding for your data. 我对使用哪种编码有一点猜测,因此您可能想要检查哪种编码对您的数据是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM