Search code examples
pythonhtmlpython-3.xcharacter-encodingcgi

Python CGI Unable To Decode Degree Sign (°)


I have an executable Python 3.6 CGI script that is running on an Apache2 server, Ubuntu 18.04.

When the script tries to execute this line: print("<p>Something about latitude x°</p>"), it throws the error:

'ascii' codec can't encode character '\xb0' in position 203: ordinal not in range(128)

Even though the encoding is specified as UTF-8 in the HTML head with <meta charset="utf-8">.

When I try to force UTF-8 on the string with .encode('utf-8'), i.e.

print("<p>Something about latitude x°</p>".encode('utf-8')), the error disappears but the degree sign shows up as UTF8 hex

Something about latitude x\xc2\xb0

I tried setting an environment variable export PYTHONIOENCODING=UTF-8 in /etc/environment and created a global variable /etc/profile.d/python-encoding.sh, then reloaded both files using source, and restarted Apache2 server systemctl restart apache2, but to no avail. CGI version: 2.6.


Solution

  • Declaring the default charset and setting the environment variable for Python encoding in Apache2 .conf did the trick, as mentioned here: Python CGI - UTF-8 doesn't work

    As pointed out by @furas.