我希望能够在编辑器中查看文件,并自动看到一个。在
# -*- 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 position 2: ordinal not in range(128)
它与有序的dict有关,与正常的dict有关。在
打开文件时没有指定编码,因此
outfile.encoding
可能是None
。在你的系统默认编码显然是ascii。在
相反,请使用所需的编码打开文件:
我曾经遇到过类似的问题,我在我的.py文件的顶部添加了这一行,它起作用了。在
相关问题 更多 >
编程相关推荐