我有一个带有非ascii字符的json文件。我正在尝试检索几个具有非ascii字符的字段,但是我遇到了关键错误。
我试过这个SO question,但没有帮助。我收到UnicodeEncodeError
错误。
JSON是:
{"Sh\u012bn\u1e0fan\u1e0f": "1","Shibirgh\u0101n": "2", "Khawr Fakk\u0101n": "3", "Sharjah": "4", "Ras al-Khaimah": "5", "Sang-e Ch\u0101rak": "6"}
我的代码:
my_dict.update({city.encode("utf-8"):id})
fobj = open("my.json",'a',0)
json.dump(m_dict,fobj)
fobj.close()
阅读json:
with io.open('my.json',encoding='utf-8') as data_file:
data = json.loads(data_file.read())
print data[u"Shibirghān".encode("utf-8")]
错误:
KeyError: 'Shibirgh\xc4\x81n
答案 0 :(得分:0)
我尝试重复你的错误,但似乎没有发生。
以下模拟代码:
#-*- coding: utf-8 -*-
import simplejson as json
#id_dict = {"Sh\u012bn\u1e0fan\u1e0f": "1","Shibirgh\u0101n": "2", "Khawr Fakk\u0101n": "3", "Sharjah": "4", "Ras al-Khaimah": "5", "Sang-e Ch\u0101rak": "6"}
id_dict = {}
encoded_city = u"Shibirghān".encode('utf-8')
id_dict.update({encoded_city: 1})
with open('my.json', 'wb') as fp:
json.dump(id_dict, fp)
jsoned_id_dict = json.dumps(id_dict)
print 'jsoned_id_dict: ', jsoned_id_dict
with open('my.json', 'rb') as data_fp:
data = json.load(data_fp)
print 'result for key %s : %d ' % (u"Shibirghān", data[u"Shibirghān"])
结果是:
jsoned_id_dict: {"Shibirgh\u0101n": 1}
result for key Shibirghān : 1
由于“Shibirghān”的结果与你的问题不一样,我想这可能与其他原因有关,除了json模块
如果被误解,请告诉我~~~