【发布时间】:2020-10-02 06:51:36
【问题描述】:
以下代码在使用 ubuntu 18 的 docker 容器中给出 UnicodeEncodeError 错误:
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
import json
text = b'["Chauss\\u00e9e de Tubize"]'
test = json.loads(text)
test
['Chauss\xe9e de Tubize'] # on other server this correctly results into ['Chaussée de Tubize']
print(test)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 8: ordinal not in range(128)
奇怪的是,这段代码 sn-p 在具有相同 python 版本(3.6.9)的 ubuntu 服务器上工作。 我不明白为什么 json 模块试图编码为 ascii。我认为从 python3 开始,标准是使用 unicode utf-8。
【问题讨论】:
标签: json python-3.x unicode utf-8