Reputation:
I use requests to get json data from API source
req = requests.get('url')
context = json.loads(req.text)
print(context)
return the error
UnicodeEncodeError at /view/
'ascii' codec can't encode characters in position 26018-26019: ordinal not in range(128)
Unicode error hint
The string that could not be encoded/decoded was: OLYURÉTHANE
I checked req.text
and didn't find there non-ascii characters. It appeards after json.loads(..)
Upvotes: 5
Views: 5191
Reputation: 849
try this:
request_txt = req.text.encode('utf-8')
context = json.loads(request_txt)
You need to apply .encode('utf-8')
to the string which is throwing this error.
Upvotes: 2