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.
Something about latitude x°
"` – furas Sep 11 '20 at 10:35