5

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(..)

3
  • The symbol É is not a ascii sysmbol. Acii character list Commented May 19, 2017 at 6:58
  • I know. how to ignore it or sort of Commented May 19, 2017 at 7:00
  • you can catch the exception and ignore it or check the ord() of each character of the string to be less than 128, and ignore these character if you still need to process the text. Commented May 19, 2017 at 7:02

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments