user4251615
user4251615

Reputation:

non 'ascii' characters in requests response

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

Answers (2)

Maninder Singh
Maninder Singh

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

Lubomir
Lubomir

Reputation: 244

Try similar like @Maninder wrote, but instead of encode() use decode()

context = json.loads(req.text.decode("utf8"))

Upvotes: 1

Related Questions