使用 json.dumps 时出现 UnicodeDecodeError
UnicodeDecodeError when using json.dumps
我正在尝试 return 具有特殊字符的 json 对象。
崩溃的行是:
return json.dumps([x.toDict() for x in searches], ensure_ascii=False)
toDict 函数:
def toDict(self):
"""Expect to dico. Needed before serialization in JSON"""
out = {}
if self.wkid is not None:
out['wkid'] = self.wkid
if self.wkt is not None:
out['wkt'] = self.wkt
return(out)
当我在 for x in searches
中打印 x 时:
for x in searches:
print x.toDict()
{'crs': {'wkid': 4326, 'wkt': 'WGS84'}, 'candidates': [{'score': 200, 'type': 'ADR', 'location': {'y': 50.2485465358886, 'x': 4.38243469412172, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': 'Fontenelle', 'munkey': '0585'}}, {'score': 200, 'type': 'ADR', 'location': {'y': 50.4123146893214, 'x': 4.32436581556278, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': "Fontaine-l'\xe3\x89v\xe3\xaaque", 'munkey': '0324'}}, {'score': 200, 'type': 'ADR', 'location': {'y': 50.3217667573625, 'x': 4.21386030471998, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': 'Fontaine-valmont', 'munkey': '0362'}}, {'score': 200, 'type': 'ADR', 'location': {'y': 49.7151404477129, 'x': 5.23438436377951, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': 'Fontenoille', 'munkey': '0541'}}], 'id': u'1', 'address': {'city': u'Fontaine-lev', 'street': u'Avenue des Chones', 'zone': u'1301', 'house': u'19'}}
这很好用。但是,当我尝试时:
for x in searches:
print json.dumps(x.toDict(), ensure_ascii=False)
我得到的错误是:
UnicodeDecodeError('ascii', '"Fontaine-l\'\xe3\x89v\xe3\xaaque"', 12, 13, 'ordinal not in range(128)')
'ascii' codec can't decode byte 0xe3 in position 12: ordinal not in range(128).
奇怪,考虑到我通过 ensure_ascii=False 来指定不应解码文本..
它仍在尝试解码文本可能有什么问题?
ensure_ascii=False
并不意味着它不会解码 unicode 文字。
If ensure_ascii is false, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.
来自 python doc
我正在尝试 return 具有特殊字符的 json 对象。 崩溃的行是:
return json.dumps([x.toDict() for x in searches], ensure_ascii=False)
toDict 函数:
def toDict(self):
"""Expect to dico. Needed before serialization in JSON"""
out = {}
if self.wkid is not None:
out['wkid'] = self.wkid
if self.wkt is not None:
out['wkt'] = self.wkt
return(out)
当我在 for x in searches
中打印 x 时:
for x in searches:
print x.toDict()
{'crs': {'wkid': 4326, 'wkt': 'WGS84'}, 'candidates': [{'score': 200, 'type': 'ADR', 'location': {'y': 50.2485465358886, 'x': 4.38243469412172, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': 'Fontenelle', 'munkey': '0585'}}, {'score': 200, 'type': 'ADR', 'location': {'y': 50.4123146893214, 'x': 4.32436581556278, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': "Fontaine-l'\xe3\x89v\xe3\xaaque", 'munkey': '0324'}}, {'score': 200, 'type': 'ADR', 'location': {'y': 50.3217667573625, 'x': 4.21386030471998, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': 'Fontaine-valmont', 'munkey': '0362'}}, {'score': 200, 'type': 'ADR', 'location': {'y': 49.7151404477129, 'x': 5.23438436377951, 'crs': {'wkid': 4326, 'wkt': 'WGS84'}}, 'address': {'city': 'Fontenoille', 'munkey': '0541'}}], 'id': u'1', 'address': {'city': u'Fontaine-lev', 'street': u'Avenue des Chones', 'zone': u'1301', 'house': u'19'}}
这很好用。但是,当我尝试时:
for x in searches:
print json.dumps(x.toDict(), ensure_ascii=False)
我得到的错误是:
UnicodeDecodeError('ascii', '"Fontaine-l\'\xe3\x89v\xe3\xaaque"', 12, 13, 'ordinal not in range(128)')
'ascii' codec can't decode byte 0xe3 in position 12: ordinal not in range(128).
奇怪,考虑到我通过 ensure_ascii=False 来指定不应解码文本..
它仍在尝试解码文本可能有什么问题?
ensure_ascii=False
并不意味着它不会解码 unicode 文字。
If ensure_ascii is false, some chunks written to fp may be unicode instances. This usually happens because the input contains unicode strings or the encoding parameter is used. Unless fp.write() explicitly understands unicode (as in codecs.getwriter()) this is likely to cause an error.
来自 python doc