jeudi 16 juin 2016

Unicode API response throwing error ''ascii' codec can't encode character u'u2019' in position 22462'

I am making an API call and the response does have unicode characters. Loading this response into a file throws the following error:

'ascii' codec can't encode character u'u2019' in position 22462

Tried all combinations of decode and encode ('utf-8').

Here is the code:

jsonfh = open(json_filename, 'w')
inter = resp.text
string_e = inter.decode('utf-8')
final = string_e.replace('\n', ' ').replace('\t', ' ').replace('\r', ' ').replace('\ ',' ')
encoded_data = final.encode('utf-8')
cleaned_data = json.loads(encoded_data)
json.dump(cleaned_data, jsonfh, indent=None)
jsonfh.close()

Lot of developers have faced this issue. a lot of places have asked to use .decode('utf-8') or having a # _*_ coding:utf-8 _*_ at the top of python.

It is still not helping.

Can someone help me with this issue.

Here is the trace:

File "/Users/SM/PycharmProjects/zendesk/zendesk_tickets_api.py", line 97, in main
string_e = inter.decode('utf-8')
File "/Users/SM/anaconda/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'u2019' in         position 22462: ordinal not in range(128)

|********************************************************|
'ascii' codec can't encode character u'u2019' in position 22462:    ordinal not in range(128)
|********************************************************|

Aucun commentaire:

Enregistrer un commentaire