问题描述
在Python 3中, stdin
和 stdout
是TextIOWrappers,有一个编码,因此吐出正常字符串字节)。
In Python 3, stdin
and stdout
are TextIOWrappers that have an encoding and hence spit out normal strings (not bytes).
我可以更改环境变量使用的编码。
I can change the encoding that is being used with an environment variable PYTHONIOENCODING. Is there also a way to change this in my script itself?
推荐答案
实际上 TextIOWrapper
返回字节。它采用Unicode字符串并返回特定编码中的字节字符串。要更改 sys.stdout
以在脚本中使用特定的编码,下面是一个示例:
Actually TextIOWrapper
does return bytes. It takes a Unicode string and returns a byte string in a particular encoding. To change sys.stdout
to use a particular encoding in a script, here's an example:
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\u5000')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\dev\python32\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u5000' in position 0: character maps to <undefined>>>> import io
>>> import io
>>> import sys
>>> sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
>>> print('\u5000')
倀
(我的终端不是UTF- 8)
(my terminal isn't UTF-8)
sys.stdout.buffer
访问原始字节流。您还可以使用以下内容以特定编码写入 stdout
:
sys.stdout.buffer
accesses the raw byte stream. You can also use the following to write to stdout
in a particular encoding:
sys.stdout.buffer.write('\u5000'.encode('utf8'))
这篇关于在Python 3中在运行时更改stdin / stdout的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!