使用json.dumps()时出现UnicodeDecodeError

我的python列表中有以下字符串(来自命令提示符):

>>> o['records'][5790]

(5790, 'Vlv-Gate-Assy-Mdl-\xe1M1-2-\xe19/16-10K-BB Credit Memo ', 60,

True, '40141613')

>>>

进一步将默认编码也更改为utf-16。但是仍然json.dumps()抛出异常如下:

>>> write(o)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "okapi_create_master.py", line 49, in write

o = json.dumps(output)

File "C:\Python27\lib\json\__init__.py", line 231, in dumps

return _default_encoder.encode(obj)

File "C:\Python27\lib\json\encoder.py", line 201, in encode

chunks = self.iterencode(o, _one_shot=True)

File "C:\Python27\lib\json\encoder.py", line 264, in iterencode

return _iterencode(o, 0)

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 25: invalid

continuation byte

无法确定此类字符串需要哪种转换才能json.dumps()正常工作。

回答:

\xe1 无法使用utf-8,utf-16编码进行解码。

>>> '\xe1'.decode('utf-8')

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode

return codecs.utf_8_decode(input, errors, True)

UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 0: unexpected end of data

>>> '\xe1'.decode('utf-16')

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "C:\Python27\lib\encodings\utf_16.py", line 16, in decode

return codecs.utf_16_decode(input, errors, True)

UnicodeDecodeError: 'utf16' codec can't decode byte 0xe1 in position 0: truncated data

尝试latin-1编码:

>>> record = (5790, 'Vlv-Gate-Assy-Mdl-\xe1M1-2-\xe19/16-10K-BB Credit Memo            ',

... 60, True, '40141613')

>>> json.dumps(record, encoding='latin1')

'[5790, "Vlv-Gate-Assy-Mdl-\\u00e1M1-2-\\u00e19/16-10K-BB Credit Memo ", 60, true, "40141613"]'

或者,指定ensure_ascii=Falsejson.dumps以使其json.dumps不尝试解码字符串。

>>> json.dumps(record, ensure_ascii=False)

'[5790, "Vlv-Gate-Assy-Mdl-\xe1M1-2-\xe19/16-10K-BB Credit Memo ", 60, true, "40141613"]'

以上是 使用json.dumps()时出现UnicodeDecodeError 的全部内容, 来源链接: utcz.com/qa/428670.html

回到顶部