Python 3 CGI:如何输出原始字节
我决定使用 Python 3 来制作我的网站,但我遇到了 Unicode 输出的问题。
看起来普通的 print(html) #html is a
str
应该可以工作,但事实并非如此。我收到 UnicodeEncodeError: 'ascii' 编解码器无法编码字符[...]: ordinal not in range(128)
。这肯定是因为网络服务器不支持 unicode 输出。
我尝试的下一件事是 print(html.encode('utf-8'))
,但我得到了类似于字节字符串的 repr 输出:它被放置在里面b'...'
并且所有转义字符均为原始形式(例如 \n
和 \xd0\x9c
)
请告诉我在 Python 3.1 中将 Unicode (str) 字符串输出为原始 UTF-8 编码 bytes 字符串的正确方法
I decided to use Python 3 for making my website, but I encountered a problem with Unicode output.
It seems like plain print(html) #html is a
str
should be working, but it's not. I get UnicodeEncodeError: 'ascii' codec can't encode characters[...]: ordinal not in range(128)
. This must be because the webserver doesn't support unicode output.
The next thing I tried was print(html.encode('utf-8'))
, but I got something like repr output of the byte string: it is placed inside b'...'
and all the escape characters are in raw form (e.g. \n
and \xd0\x9c
)
Please show me the correct way to output a Unicode (str) string as a raw UTF-8 encoded bytes string in Python 3.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是你的 stdout 没有连接到实际的终端,并且默认使用 ASCII 编码。因此,您需要写入 sys.stdout.buffer,它是 sys.stdout 的“原始”二进制输出。这可以通过多种方式完成,最常见的一种似乎是:
使用编写器。在 CGI 脚本中,您可以将 sys.stdout 替换为 writer,这样:
可能实际上可以工作,以便您可以正常打印。试试吧!
The problem here is that you stdout isn't attached to an actual terminal and will use the ASCII encoding by default. Therefore you need to write to sys.stdout.buffer, which is the "raw" binary output of sys.stdout. This can be done in various ways, the most common one seems to be:
And the use writer. In a CGI script you may be able to replace sys.stdout with the writer so:
Might actually work so you can print normally. Try that!