python - “UnicodeEncodeError: 'ascii' codec can't encode character” in Python3 -


i'm fetching json requests api (using python 3.5) , when i'm trying print (or use) json, either response.text, json.loads(...) or response.json(), unicodeencodeerror.

print(response.text) unicodeencodeerror: 'ascii' codec can't encode character '\xc5' in position 676: ordinal not in range(128) 

the json contains array of dictionaries country names , of them contain special characters, e.g.: (just 1 dictionary in binary array example)

b'[{\n "name" : "\xc3\x85land islands"\n}] 

i have no idea why there encoding problem , why "ascii" used when requests detects utf-8 encoding (and setting manually utf-8 doesn't change anything).

edit2: problem microsoft visual studio code 1.4. wasn't able print characters.

if code running within vs, sounds python can't work out encoding of inbuilt console, defaults ascii. if try print non-ascii python throws error rather printing text won't display.

you can force python's encoding using pythonioencoding environment variable. set within run configuration script.

depending on visual studio's console, may away with:

pythonioencoding=utf-8 

or may have use typical 8bit charset like:

pythonioencoding=windows-1252 

Comments