【发布时间】:2011-07-27 18:22:24
【问题描述】:
我决定使用 Python 3 来制作我的网站,但遇到了 Unicode 输出问题。
看起来普通的print(html) #html is astr 应该可以工作,但事实并非如此。我得到UnicodeEncodeError: 'ascii' codec can't encode characters[...]: ordinal not in range(128)。这一定是因为网络服务器不支持 unicode 输出。
接下来我尝试的是print(html.encode('utf-8')),但我得到了字节字符串的repr 输出:它被放置在b'...' 中,并且所有转义字符都是原始形式(例如\n 和 \xd0\x9c)
请告诉我在 Python 3.1 中将 Unicode (str) 字符串输出为原始 UTF-8 编码 bytes 字符串的正确方法
【问题讨论】:
-
请注意,Python 3.1 上的 cgi 和 wsgi 包存在问题。 Python 3 直到 3.2 才真正可用于 Web 应用程序。
标签: unicode encoding utf-8 python-3.x webserver