使用 json.dumps() 时出现 UnicodeDecodeError [英] UnicodeDecodeError while using json.dumps()

查看:29
本文介绍了使用 json.dumps() 时出现 UnicodeDecodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>>>>o['记录'][5790](5790, 'Vlv-Gate-Assy-Mdl-xe1M1-2-xe19/16-10K-BB Credit Memo', 60,是的,'40141613')>>>

我尝试过这里提到的建议:更改 Python 的默认编码?

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

<预><代码>>>>写(o)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件okapi_create_master.py",第 49 行,写入o = json.dumps(输出)转储中的文件C:Python27libjson\__init__.py",第 231 行返回_default_encoder.encode(obj)文件C:Python27libjsonencoder.py",第 201 行,编码块 = self.iterencode(o, _one_shot=True)文件C:Python27libjsonencoder.py",第 264 行,在 iterencode 中返回 _iterencode(o, 0)UnicodeDecodeError: 'utf8' 编解码器无法解码位置 25 中的字节 0xe1:无效连续字节

无法确定这些字符串需要什么样的转换才能使 json.dumps() 工作.

解决方案

xe1 is not decodeable using utf-8, utf-16 encoding.

>>>'xe1'.decode('utf-8')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:Python27libencodingsutf_8.py",第 16 行,解码中返回 codecs.utf_8_decode(输入,错误,真)UnicodeDecodeError: 'utf8' 编解码器无法解码位置 0 中的字节 0xe1:数据意外结束>>>'xe1'.decode('utf-16')回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:Python27libencodingsutf_16.py",第 16 行,解码中返回 codecs.utf_16_decode(input, errors, True)UnicodeDecodeError: 'utf16' 编解码器无法解码位置 0 中的字节 0xe1:截断的数据

尝试 latin-1 编码:

<预><代码>>>>记录 = (5790, 'Vlv-Gate-Assy-Mdl-xe1M1-2-xe19/16-10K-BB Credit Memo',... 60, 真, '40141613')>>>json.dumps(record, encoding='latin1')'[5790, "Vlv-Gate-Assy-Mdl-\u00e1M1-2-\u00e19/16-10K-BB Credit Memo", 60, true, "40141613"]'

或者,指定 ensure_ascii=False, json.dumps 使 json.dumps 不尝试解码字符串.

<预><代码>>>>json.dumps(记录,ensure_ascii=False)'[5790, "Vlv-Gate-Assy-Mdl-xe1M1-2-xe19/16-10K-BB Credit Memo", 60, true, "40141613"]'

I have strings as follows in my python list (taken from command prompt):

>>> o['records'][5790]
(5790, 'Vlv-Gate-Assy-Mdl-xe1M1-2-xe19/16-10K-BB Credit Memo            ', 60,
 True, '40141613')
>>>

I have tried suggestions as mentioned here: Changing default encoding of Python?

Further changed the default encoding to utf-16 too. But still json.dumps() threw and exception as follows:

>>> 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:Python27libjson\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:Python27libjsonencoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:Python27libjsonencoder.py", line 264, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 25: invalid
continuation byte

Can't figure what kind of transformation is required for such strings so that json.dumps() works.

解决方案

xe1 is not decodable using utf-8, utf-16 encoding.

>>> 'xe1'.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Python27libencodingsutf_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:Python27libencodingsutf_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

Try latin-1 encoding:

>>> 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"]'

Or, specify ensure_ascii=False, json.dumps to make json.dumps not try to decode the string.

>>> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆