【发布时间】:2015-01-30 22:41:02
【问题描述】:
我希望能够在编辑器中查看文件并自动看到一个 ü。
# -*- coding: utf-8 -*-
import json
from collections import OrderedDict
fdata = OrderedDict()
fdata[u"Züge"] = 0
fdata[u"Bahnhöfe"] = 0
with open("Desktop/test.json", "w") as outfile:
json.dump(fdata, outfile, indent=2, ensure_ascii=False)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in 位置 2:序数不在范围内(128)
它与 OrderedDict 有关,使用普通的 dict 即可。
【问题讨论】:
-
异常仅在运行时发生还是您也难以在文本编辑器中显示字符?
-
当我尝试 json.dumps() 时发生
-
你能告诉我们打开“文件”的代码吗?你指定了编码吗?
标签: python json python-2.7