如何在 print() 语句中指定编码?
How do I specify the encoding in an print() statement?
如何在 print()
语句中指定编码?
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
print()
calls file.write()
, and file
defaults to sys.stdout
. sys.stdout
is a file object whose write()
method encodes strings according to its encoding property. If you reconfigure 属性 它将改变字符串在打印时的编码方式:
sys.stdout.reconfigure(encoding='latin-1')
或者,您可以自己对字符串进行编码,然后将字节写入标准输出的底层二进制文件 buffer。
sys.stdout.buffer.write("<some text>".encode('latin-1'))
注意 buffer
不是 public 属性:“这不是 TextIOBase API 的一部分,在某些实现中可能不存在。”
如何在 print()
语句中指定编码?
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
print()
calls file.write()
, and file
defaults to sys.stdout
. sys.stdout
is a file object whose write()
method encodes strings according to its encoding property. If you reconfigure 属性 它将改变字符串在打印时的编码方式:
sys.stdout.reconfigure(encoding='latin-1')
或者,您可以自己对字符串进行编码,然后将字节写入标准输出的底层二进制文件 buffer。
sys.stdout.buffer.write("<some text>".encode('latin-1'))
注意 buffer
不是 public 属性:“这不是 TextIOBase API 的一部分,在某些实现中可能不存在。”