Python 如何在 Windows 控制台中显示 utf-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3578685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:47:36  来源:igfitidea点击:

How to display utf-8 in windows console

pythonwindowsutf-8console

提问by russo

I'm using Python 2.6 on Windows 7

我在 Windows 7 上使用 Python 2.6

I borrowed some code from here: Python, Unicode, and the Windows console

我从这里借用了一些代码: Python、Unicode 和 Windows 控制台

My goal is to be able to display uft-8 strings in the windows console.

我的目标是能够在 Windows 控制台中显示 uft-8 字符串。

Apparantly in python 2.6, the

显然在 python 2.6 中,

sys.setdefaultencoding()

sys.setdefaultencoding()

is no longer supported

不再支持

However, I wrote reload(sys) before I tried to use it and it magically didn't error.

但是,我在尝试使用它之前编写了 reload(sys) 并且它神奇地没有出错。

This code will NOT error, but it shows funny characters instead of japanese text. I believe the problem is because I have not successfully changed the codepage of the windows console.

此代码不会出错,但会显示有趣的字符而不是日语文本。 我相信问题是因为我没有成功更改 Windows 控制台的代码页。

These are my attempts, but they don't work:

这些是我的尝试,但它们不起作用:

reload(sys)
sys.setdefaultencoding('utf-8')

print os.popen('chcp 65001').read()

sys.stdout.encoding = 'cp65001'

Perhaps you can use win32console to change the codepage?I tried the code from the website I linked, but it also errored from the win32console.. maybe that code is obsolete.

也许您可以使用 win32console 更改代码页?我尝试了我链接的网站上的代码,但它也从 win32console 中出错了。也许该代码已过时。

Here's my code, that doesn't error but prints funny characters:

这是我的代码,它不会出错,但会打印有趣的字符:

#coding=<utf8>
import os
import sys
import codecs



reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

#print os.popen('chcp 65001').read()
print(sys.stdout.encoding)
sys.stdout.encoding = 'cp65001'
print(sys.stdout.encoding)

x = raw_input('press enter to continue')

a = 'こんにちは世界'#.decode('utf8')
print a

x = raw_input()

回答by habnabit

Never evereveruse setdefaultencoding. If you want to write unicode strings to stdio, encode them explicitly. Monkeying around with setdefaultencodingwill cause stdlib modules and third-party modules alike to break in horrible subtle ways by allowing implicit conversion between strand unicodewhen it shouldn't happen.

从来没有曾经使用setdefaultencoding。如果您想将 unicode 字符串写入 stdio,请对其进行显式编码。Monkeying withsetdefaultencoding将导致 stdlib 模块和第三方模块以可怕的微妙方式破坏strunicode因为它允许在不应该发生的时间和时间之间进行隐式转换。

Yes, the problem is most likely that your code page isn't set properly. However, using os.popenwon't change the code page; it'll spawn a new shell, change itscode page, and then immediately exit without affecting your console at all. I'm not personally very familiar with windows, so I couldn't tell you how to change your console's code page from within your python program.

是的,问题很可能是您的代码页设置不正确。但是,使用os.popen不会改变代码页;它会生成一个新的 shell,更改它的代码页,然后立即退出,根本不会影响您的控制台。我个人对 Windows 不是很熟悉,所以我无法告诉你如何从你的 python 程序中更改控制台的代码页。

The way to properly display unicode data via utf-8 from python, as mentioned before, is to explicitly encode your strings before printing them: print s.encode('utf-8')

如前所述,从 python 通过 utf-8 正确显示 unicode 数据的方法是在打印字符串之前对它们进行显式编码: print s.encode('utf-8')

回答by Mark Tolonen

Windows doesn't support UTF-8 in a console properly. The only way I know of to display Japanese in the console is by changing (on XP) Control Panel's Regional and Language Options, Advanced Tab, Language for non-Unicode Programs to Japanese. After rebooting, open a console and run "chcp" to find out the Japanese console's code page. Then either print Unicode strings or byte strings explicitly encoded in the correct code page.

Windows 在控制台中不正确支持 UTF-8。我知道在控制台中显示日语的唯一方法是将(在 XP 上)控制面板的区域和语言选项、高级选项卡、非 Unicode 程序的语言更改为日语。重新启动后,打开控制台并运行“chcp”以找出日语控制台的代码页。然后打印以正确代码页显式编码的 Unicode 字符串或字节字符串。

回答by Daira Hopwood

Changing the console code page is both unnecessary and won't work (in particular, setting it to 65001 runs into a Python bug). See this questionfor details, and for how to print Unicode characters to the console regardless of the code page.

更改控制台代码页既不必要又不起作用(特别是,将其设置为 65001 会导致Python 错误)。有关详细信息以及如何在不考虑代码页的情况下将 Unicode 字符打印到控制台,请参阅此问题

回答by Mark Ransom

I know you state you're using Python 2.6, but if you're able to use Python 3.3 you'll find that this is finally supported.

我知道你说你使用的是 Python 2.6,但如果你能够使用 Python 3.3,你会发现这最终得到了支持。

Use the command chcp 65001before starting Python.

chcp 65001在启动 Python 之前使用该命令。

See http://docs.python.org/dev/whatsnew/3.3.html#codecs

请参阅http://docs.python.org/dev/whatsnew/3.3.html#codecs

In Python 3.6 it's no longer even necessary to use the chcpcommand, since Python bypasses the byte-level console interface entirely and uses a native Unicode interface instead. See PEP 528: Change Windows console encoding to UTF-8.

在 Python 3.6 中,甚至不再需要使用该chcp命令,因为 Python 完全绕过了字节级控制台接口,而是使用本机 Unicode 接口。请参阅PEP 528:将 Windows 控制台编码更改为 UTF-8

As noted in the comments by @mbom007, it's also important to make sure the console is configured with a font that supports the characters you're trying to display.

正如@mbom007 的评论中所指出的,确保控制台配置的字体支持您尝试显示的字符也很重要。