如何制作python 3 print()utf8

dav*_*puh 43 unicode encoding stdout utf-8 python-3.x

如何print("Some text")在UTF-8中使用python 3(3.1)到stdout,或者如何输出原始字节?

Test.py

TestText = "Test - ??????..šŠ??žŽ" # this is UTF-8
TestText2 = b"Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd" # just bytes
print(sys.getdefaultencoding())
print(sys.stdout.encoding)
print(TestText)
print(TestText.encode("utf8"))
print(TestText.encode("cp1252","replace"))
print(TestText2)
Run Code Online (Sandbox Code Playgroud)

输出(在CP1257和I中将字符替换为字节值[x00]):

utf-8
cp1257
Test - [xE2][xC2][xE7][C7][xE8][xC8]..[xF0][xD0][xFB][xDB][xFE][xDE]  
b'Test - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
b'Test - ??????..\x9a\x8a??\x9e\x8e'
b'Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
Run Code Online (Sandbox Code Playgroud)

print太聪明了......:D使用编码文本是没有意义的print(因为它总是只显示字节的表示而不是实际字节)并且根本不可能输出字节,因为无论如何打印并始终对其进行编码sys.stdout.encoding.

例如:print(chr(255))抛出错误:

Traceback (most recent call last):
  File "Test.py", line 1, in <module>
    print(chr(255));
  File "H:\Python31\lib\encodings\cp1257.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xff' in position 0: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

顺便说print( TestText == TestText2.decode("utf8"))回报False,但打印输出是一样的.


Python 3 sys.stdout.encoding如何确定以及如何更改它?

我做了一个printRAW()工作正常的函数(实际上它将输出编码为UTF-8,所以它真的不是原始的......):

 def printRAW(*Text):
     RAWOut = open(1, 'w', encoding='utf8', closefd=False)
     print(*Text, file=RAWOut)
     RAWOut.flush()
     RAWOut.close()

 printRAW("Cool", TestText)
Run Code Online (Sandbox Code Playgroud)

输出(现在以UTF-8打印):

Cool Test - ??????..šŠ??žŽ
Run Code Online (Sandbox Code Playgroud)

printRAW(chr(252))也很好地打印ü(在UTF-8中[xC3][xBC]),没有错误:)

现在我正在寻找更好的解决方案,如果有的话......

Mar*_*nen 56

一,修正:

TestText = "Test - ??????..šŠ??žŽ" # this NOT utf-8...it is a Unicode string in Python 3.X.
TestText2 = TestText.encode('utf8') # THIS is "just bytes" in UTF-8.
Run Code Online (Sandbox Code Playgroud)

现在,要将UTF-8发送到stdout,无论控制台的编码如何,请使用适合该作业的工具:

import sys
sys.stdout.buffer.write(TestText2)
Run Code Online (Sandbox Code Playgroud)

"buffer"是stdout的原始接口.


zwo*_*wol 15

这是我从手册中可以做到的最好的,这是一个肮脏的黑客:

utf8stdout = open(1, 'w', encoding='utf-8', closefd=False) # fd 1 is stdout
print(whatever, file=utf8stdout)
Run Code Online (Sandbox Code Playgroud)

看起来文件对象应该有一个方法来改变它们的编码,但是AFAICT没有一个.

如果你写入utf8stdout然后写入sys.stdout而不先调用utf8stdout.flush(),反之亦然,可能会发生坏事.

  • 在 windows 上有问题,其中 `cp1257` 用于打印(失败),而我想要 `utf-8`。以下代码段有效:`import sys; sys.stdout = open(1, 'w', encoding='utf-8', closefd=False); 打印(“vadsэавфыаЭХÜÜÄ”);打印(字节(“аЭХÜ”,“utf-8”))` (3认同)

Cer*_*vEd 13

根据这个答案

您可以手动重新配置 stdout 的编码python 3.7

import sys
sys.stdout.reconfigure(encoding='utf-8')
Run Code Online (Sandbox Code Playgroud)