UnicodeDecodeError while using json.dumps()(使用 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() 抛出异常如下:
无法确定这些字符串需要什么样的转换才能使 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"]'或者,指定
<预><代码>>>>json.dumps(记录,ensure_ascii=False)'[5790, "Vlv-Gate-Assy-Mdl-xe1M1-2-xe19/16-10K-BB Credit Memo", 60, true, "40141613"]'ensure_ascii=False
,json.dumps
使json.dumps
不尝试解码字符串.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 json.dumps() 时出现 UnicodeDecodeError


基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01