Python 2.7:在 Windows 控制台中输出 utf-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7078232/
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
Python 2.7: output utf-8 in Windows console
提问by Nikolai
Let's say
让我们说
s = u"test\u0627\u0644\u0644\u0647 \u0623\u0643\u0628\u0631\u7206\u767A\u043E\u043B\u043E\u043B\u043E"
If I try to print it directly,
如果我尝试直接打印它,
>>> print s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'cp932' codec can't encode character u'\u0627' in position 4: illegal multibyte sequence
So I change the console into UTF-8 from within Python (otherwise it won't understand my input).
所以我从 Python 中将控制台更改为 UTF-8(否则它不会理解我的输入)。
import win32console
win32console.SetConsoleOutputCP(65001)
win32console.SetConsoleCP(65001)
And then output the string encoded as utf-8, because Python doesn't know that chcp 65001 is UTF-8 (a known bug).
然后输出编码为 utf-8 的字符串,因为 Python 不知道 chcp 65001 是 UTF-8(一个已知错误)。
>>> print s.encode('utf-8')
test???? ????爆発ололоTraceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 0] Error
As you can see, it prints successfully until it hits a newline, then it throws an IOError.
如您所见,它会成功打印,直到遇到换行符,然后抛出 IOError。
The following workaround works:
以下解决方法有效:
def safe_print(str):
try:
print str.encode('utf-8')
except:
pass
print
>>> safe_print(s)
test???? ????爆発ололо
But there must be a better way. Any suggestions?
但必须有更好的方法。有什么建议?
回答by Piotr Dobrogost
SearchingSO for python utf8 windowsbrings as the first result the question Getting python to print in UTF8 on Windows XP with the consolewhich describes what's the problem with printing utf8 in Windows from Python.
搜索SO for python utf8 windows带来的第一个结果是使用控制台在 Windows XP 上使用 UTF8 打印 python的问题,该控制台描述了从 Python 在 Windows 中打印 utf8 的问题。
回答by dmitry_romanov
I didn't test it on windows, but hereyou can get small initialization script for both win/linux to setup output encoding properly, including logging interface, etc. The module also makes output colored (including update of 'logging' interface)? but you can cut it off unnecessary functionality easily :-).
我没有在windows上测试过,但是在这里你可以获得win/linux的小初始化脚本来正确设置输出编码,包括日志界面等。该模块还使输出有颜色(包括“日志”界面的更新)?但是您可以轻松地将其切断不必要的功能:-)。
How to invoke non-colored variant:
如何调用非彩色变体:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setupcon import setup_console
setup_console('utf-8', False)
and colored variant:
和彩色变体:
import setupcon
setupcon.setup_console()
import logging
#...
if setupcon.ansi:
logging.getLogger().addHandler(setupcon.ColoredHandler())
If the solution works for you, you can either read the documentation here: http://habrahabr.ru/blogs/python/117236/, in Russian, or I/somebody can translate it for you on demand :-).
如果该解决方案适合您,您可以阅读此处的文档:http: //habrahabr.ru/blogs/python/117236/,俄语版,或者我/某人可以按需为您翻译:-)。