如何使用Python 3打印特殊字符?

时间:2015-02-22 02:42:30

标签: python python-3.x unicode ascii

我正在尝试用Python 3编写一个简单的CGI脚本,它将打印字符串“MÅDRS”(注意Å)。以下代码工作正常:

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

但是,当我尝试打印“MÅDRS”而不是“Test”时,我收到以下错误:

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)

这个问题有一个简单的解决方案吗?我已经研究了几个小时但似乎无法找到它。

0 个答案:

没有答案