0

I am trying to write a simple CGI script with Python 3 that will print the string "MÅDRS" (note the Å). The following code works fine:

print("Content-type: text/html")
print("\n")
print("Test")

However, when I attempt to print "MÅDRS" instead of "Test" I receive the following error:

Traceback (most recent call last):
File "index.cgi", line 69, in <module>
File "index.cgi", line 51, in main
File "/usr/local/lib/python3.0/io.py", line 1494, in write
    b = encoder.encode(s)
File "/usr/local/lib/python3.0/encodings/ascii.py", line 22, in encode
    return codecs.ascii_encode(input, self.errors)[0]
UnicodeEncodeError: 'ascii' codec can't encode character '\xc5' in position 1: ordinal not in range(128)

Is there a simple solution to this problem? I have been researching for several hours but can't seem to find it.

Michael
  • 1
  • 1
  • this is very likely related to your use of the `cgi` module. why are you using `cgi`, btw? it's a brittle and ridiculously outdated technology; even if you have no other way to actually serve pages, you should strongly consider using something like [flask](http://flask.pocoo.org/), which should know how to handle non-ascii output. – Eevee Feb 22 '15 at 02:45
  • 2
    If you are **really** using Python 3.0, then run, do not walk, to python.org and download a newer version. 3.0 had tons of issues, and is no longer supported. Upgrade! – MattDMo Feb 22 '15 at 02:46
  • oh, yikes, good catch :) – Eevee Feb 22 '15 at 02:47

0 Answers0