Python 3 CGI:如何输出原始字节

编程入门 行业动态 更新时间:2024-10-26 14:30:20
本文介绍了Python 3 CGI:如何输出原始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我决定使用Python 3让我的网站,但我遇到的Unicode输出的问题。

I decided to use Python 3 for making my website, but I encountered a problem with Unicode output.

它看起来像普通的打印( html)#html是一个 str 应该工作,但不是。我得到 UnicodeEncodeError:'ascii'编解码器不能编码字符[...]:序号不在范围(128)。这是因为网络服务器不支持unicode输出。

It seems like plain print(html) #html is astr 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.

我尝试的下一件事是 print(html.encode('utf-8 ')),但我得到了类似的再版的字节串的输出:它被放置在 b' ...和所有的转义字符是原始形式(例如 \\\ 和 \xd0\x9c )

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)

请告诉我正确的方式输出一个Unicode( STR )字符串作为原料UTF-8编码的字节 string in Python 3.1

Please show me the correct way to output a Unicode (str) string as a raw UTF-8 encoded bytes string in Python 3.1

推荐答案

这里的问题是,您的标准输出不附加到实际的终端,并将使用默认情况下为ASCII编码。因此,您需要写入sys.stdout.buffer,这是sys.stdout的raw二进制输出。这可以通过各种方式来完成,最常见的似乎是:

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:

import codecs, sys writer = codecs.getwriter('utf8')(sys.stdout.buffer)

和使用作者。在CGI脚本中,您可以使用写入器替换sys.stdout:

And the use writer. In a CGI script you may be able to replace sys.stdout with the writer so:

sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)

实际上可能工作,从而就可以正常打印。试试!

Might actually work so you can print normally. Try that!

更多推荐

Python 3 CGI:如何输出原始字节

本文发布于:2023-10-24 09:24:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   原始   Python   CGI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!