使用控制台在 Windows XP 上使用 UTF8 打印 python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7014430/
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
Getting python to print in UTF8 on Windows XP with the console
提问by Philippe F
I would like to configure my console on Windows XP to support UTF8 and to have python detect that and work with it.
我想在 Windows XP 上配置我的控制台以支持 UTF8 并让 python 检测并使用它。
So far, my attempts:
到目前为止,我的尝试:
C:\Documents and Settings\Philippe>C:\Python25\python.exe
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'é'
é
>>> import sys
>>> sys.stdout.encoding
'cp437'
>>> quit()
So, by default I am in cp437 and python detects that just fine.
所以,默认情况下我在 cp437 中,python 检测到就好了。
C:\Documents and Settings\Philippe>chcp 65001
Active code page: 65001
C:\Documents and Settings\Philippe>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'cp65001'
>>> print u'é'
C:\Documents and Settings\Philippe>
It seems like printing in UTF8 makes python crash now...
现在似乎以 UTF8 打印使 python 崩溃......
采纳答案by bobince
I would like to configure my console on Windows XP to support UTF8
我想在 Windows XP 上配置我的控制台以支持 UTF8
I don't think it's going to happen.
我不认为这会发生。
The 65001 code page is buggy; some stdio calls behave incorrectly and break many tools. Whilst you can register cp65001 as an encoding manually:
65001 代码页有问题;一些 stdio 调用行为不正确并破坏了许多工具。虽然您可以手动将 cp65001 注册为编码:
def cp65001(name):
if name.lower()=='cp65001':
return codecs.lookup('utf-8')
codecs.register(cp65001)
and this allows you to print u'some unicode string'
, it doesn't allow you to write non-ASCII characters in that Unicode string. You get the same odd errors (IOError 0 et al) that you do when you try to write non-ASCII UTF-8 sequences directly as byte strings.
这允许您print u'some unicode string'
,它不允许您在该 Unicode 字符串中写入非 ASCII 字符。当您尝试将非 ASCII UTF-8 序列直接编写为字节字符串时,您会遇到相同的奇怪错误(IOError 0 等)。
Unfortunately UTF-8 is a second-class citizen under Windows. NT's Unicode model was drawn up before UTF-8 existed and consequently you're expected to use two-byte-per-code-unit encodings (UTF-16, originally UCS-2) anywhere you want consistent Unicode. Using byte strings, like many portable apps and languages (such as Python) written with C's stdio
, doesn't fit that model.
不幸的是,UTF-8 在 Windows 下是二等公民。NT 的 Unicode 模型是在 UTF-8 存在之前制定的,因此您应该在任何需要一致 Unicode 的地方使用每代码单元两个字节的编码(UTF-16,最初是 UCS-2)。使用字节字符串,就像许多用 C 编写的便携式应用程序和语言(如 Python)一样stdio
,不适合该模型。
And rewriting Python to use the Windows Unicode console calls (like WriteConsoleW) instead of the portable C stdio ones doesn't play well with shell tricks like piping and redirecting to a file. (Not to mention that you still have to change from the default terminal font to a TTF one before you can see the results working at all...)
重写 Python 以使用 Windows Unicode 控制台调用(如 WriteConsoleW)而不是可移植的 C stdio 调用并不能很好地与 shell 技巧(如管道和重定向到文件)配合使用。(更不用说您仍然必须从默认终端字体更改为 TTF 字体,然后才能看到完全有效的结果......)
Ultimately if you need a command line with working UTF-8 support for stdio-based apps, you'd probably be better off using an alternative to the Windows Console that deliberately supports it, such as Cygwin's, or Python's IDLE or pywin32's PythonWin.
最终,如果您需要一个对基于 stdio 的应用程序提供工作 UTF-8 支持的命令行,您可能最好使用故意支持它的 Windows 控制台的替代品,例如 Cygwin 的、Python 的 IDLE 或 pywin32 的 PythonWin。
回答by Mark Ransom
When I try the same thing on Python 2.7 I get an error on import sys
:
当我在 Python 2.7 上尝试同样的事情时,出现以下错误import sys
:
LookupError: unknown encoding: cp65001
查找错误:未知编码:cp65001
This implies to me that Python doesn't know how to work with the special Windows UTF-8 code page, and 2.5 handled the situation ungracefully.
这对我来说意味着 Python 不知道如何使用特殊的 Windows UTF-8 代码页,而 2.5 处理这种情况很不雅观。
Apparently this was investigated and notfixed in Python 3.2: http://bugs.python.org/issue6058
显然这是在 Python 3.2 中进行了调查并没有修复的:http: //bugs.python.org/issue6058
Update:In What's New In Python 3.3it lists cp65001
support as a new feature.
更新:在Python 3.3cp65001
的新增功能中,它将支持列为一项新功能。
回答by iBob101
I had problems displaying the Euro symbol in the cmd console from a Python script using Windows Vista. Here's what worked for me:
我在使用 Windows Vista 的 Python 脚本中在 cmd 控制台中显示欧元符号时遇到问题。这是对我有用的:
Fist, I need to make sure the font is set as Lucinda Console
and not Raster Fonts which don't work. That can be done by setting the default properties of the console in the drop down menu of the console window and restarting the console window with cmd.exe
.
拳头,我需要确保字体设置为Lucinda Console
而不是不起作用的光栅字体。这可以通过在控制台窗口的下拉菜单中设置控制台的默认属性并使用 重新启动控制台窗口来完成cmd.exe
。
Second, when I run cmd I set the code page with chcp 1252
.
其次,当我运行 cmd 时,我将代码页设置为chcp 1252
.
Third, I make sure my editor (Notepad++) has the right encoding settings. On the Encoding
drop down menu in Notepad++ select Encode in UTF-8
.
第三,我确保我的编辑器 (Notepad++) 具有正确的编码设置。Encoding
在 Notepad++的下拉菜单中选择Encode in UTF-8
.
That worked for me.
那对我有用。
回答by YRUsoDiao
set this in your win:
在你的胜利中设置:
set PYTHONIOENCODING=utf-8