pythonjsonunicodepython-unicode

UnicodeEncodeError on API-call (json)


I am trying to print out the result of this API-call, but I am getting an UnicodeEncodeError. Probably super noob question, but would really appreciate any help with this :)

import http.client
import json

api_key = 'hidden'
connection = http.client.HTTPConnection('api.football-data.org')
headers = { 'X-Auth-Token': api_key, 'X-Response-Control': 'minified' }
connection.request('GET', '/v1/competitions', None, headers)
response = json.loads(connection.getresponse().read().decode())

print(response)

Error:

Traceback (most recent call last): File "/Users/kjetilbergtun/Dropbox/My Python Projects/footballapi.py", line 13, in print(response)

UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 51: ordinal not in range(128)


Solution

  • encode is used by print to convert the Unicode characters in your string to a byte stream that can be sent to your output device.

    Before you start Python, you can set the environment variable PYTHONIOENCODING to the encoding required by your console. I'd recommend trying mbcs on Windows and utf-8 everywhere else if you don't know what that should be. If you don't provide an encoding the default will be ascii, which only works on the simplest strings.


    The above advice is a bit obsolete. As of Python 3.6 for Windows, there's no need to use any encoding except utf-8 which is now the default. The native byte encoding of your console doesn't matter because Python bypasses it with native API calls capable of handing all Unicode characters. For other operating systems the default is probably utf-8 as well, although you can check for yourself by looking at the value of sys.stdout.encoding. This is especially important if your output is being redirected to a file.