Python序列化E

2024-03-29 10:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

当我的python代码试图将dictionary对象转换为Json字符串时,它引发了以下异常:

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

有什么解决这个问题的提示吗。在


Tags: 对象字符串代码namejsondictionarystatusrd
1条回答
网友
1楼 · 发布于 2024-03-29 10:18:45

默认情况下,json.dump()使用UTF8编码,但是字典中name键的值不是UTF8。它看起来像ISO-8859-X编码之一。您可以使用encoding参数指定编码:

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

输出

^{pr2}$

我对使用哪种编码有点猜测,所以您可能需要检查哪种编码对您的数据是正确的。在

相关问题 更多 >