我认为sys.stdout.encoding
将是了解print
使用的编码的明确方式,但这显然是错误的:
>>> hello_in_chinese = '\u4f60\u597d'
>>> print(sys.stdout.encoding, hello_in_chinese)
cp1252 你好
>>> hello_in_chinese.encode(sys.stdout.encoding)
Traceback (most recent call last):
File "<pyshell#65>", line 1, in <module>
hello_in_chinese.encode(sys.stdout.encoding)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\encodings\cp1252.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined>
在Windows 7上,这是在Python 3.6.3下的IDLE。
如何获得实际使用的编码?