从cmd.exe运行时,Python的stdout中的Unicode输出 [英] Unicode output in Python's stdout when running from cmd.exe

查看:271
本文介绍了从cmd.exe运行时,Python的stdout中的Unicode输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Windows 7,其控制台已配置为使用Consolas字体,这使我有可能输出Unicode。在控制台中读取Unicode的能力已经被许多程序证明了,例如远程管理器:可以在同一个字符串中的同一个控制台上读取西里尔语和德语äöü字母,而无需编码切换。



现在关于Python。



我正在努力尝试,但在输出中看不到Unicode。
默认情况下 print(sys.stdout.encoding)打印 cp866 ,stdout无法输出任何字符除了ASCII和西里尔文。



它给出以下结果:



print( UnicodeЛcode>>лл>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -12 -12 -12 -12 -12 -12 -12 -12 -12 -12 -12 -12 -12 :字符映射到< undefined>



print(Ля-ляäöüÄÖÜß.encode(utf- 8))



b'\xd0\x9b\xd1\x8f-\xd0 \xbb\xd1\x8f \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\好的,我已经在批处理文件中设置了 PYTHONIOENCODING 环境变量:



SET PYTHONIOENCODING = UTF-8



并得到:

  print(sys.stdout.encoding)
UTF-8

print(Ля-ляäöüÄÖÜß)
╨Ы╤П-╨╗╤П├д├╢├╝├Д├Ц├Ь├Я

print( Ля-ляäöüÄÖÜß.encode(utf-8))`
b'\xd0\x9b\xd1\x8f- \xd0\xbb\xd1\x8f \ xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f'

该怎么办?

解决方案

,Python和Windows控制台之间的交互有一个错误(参见 http://bugs.python.org/issue1602)。可以使用C函数ReadConsoleW,WriteConsoleW而不是ReadConsole和WriteConsole在Windows控制台中读写Unicode。所以一个似乎有效的解决方案是编写自己的stdout和stdin对象,通过ctypes调用ReadConsoleW,WriteConsoleW。对于输出,这个工作原理,但是对于输入来说,Python交互式解释器实际上并没有使用sys.stdin来获取输入(但是调用input()函数),所以有一个问题 - 参见 http://bugs.python.org/issue17620



许多人说Windows有问题安慰。但是你可以键入Unicode字符(如果你有正确的键盘布局)没有问题。这些显示没有问题。您甚至可以使用一些Unicode参数运行名为∫.py的文件,并且正确运行,并在sys.argv字符串中等待参数相关参数。



更新:我已经构建了一个Python包来处理这些问题。请参阅 https://github.com/Drekin/win-unicode-console https://pypi.python.org/pypi/win_unicode_console 。安装 pip install win_unicode_console 。至少对于Python 3.4,Python 3.5和Python 2.7来说,它至关重要。


I am running Windows 7 and its console has been configured to use Consolas font, which gives me a possibility of Unicode output. The ability to read Unicode in console has been proved by me many times for programs such as Far Manager: both Cyrillics and German äöü letters can be read on the same console in the same string without encoding switching.

Now about Python.

I am trying very hard, but can't see Unicode in it's output. By default print(sys.stdout.encoding) prints cp866 and stdout is unable to output any characters except ASCII and Cyrillics.

It gives me following results:

print("Ля-ля äöüÄÖÜß")

UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-12: character maps to <undefined>

print("Ля-ля äöüÄÖÜß".encode("utf-8"))

b'\xd0\x9b\xd1\x8f-\xd0\xbb\xd1\x8f \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f'

Ok, I've set the PYTHONIOENCODING environment variable in batch file:

SET PYTHONIOENCODING=UTF-8

and got:

print(sys.stdout.encoding)
UTF-8

print("Ля-ля äöüÄÖÜß")
╨Ы╤П-╨╗╤П ├д├╢├╝├Д├Ц├Ь├Я

print("Ля-ля äöüÄÖÜß".encode("utf-8"))`
b'\xd0\x9b\xd1\x8f-\xd0\xbb\xd1\x8f \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f'

What to do?

解决方案

Actually, there's a kinda bug in interaction between Python and Windows console (see http://bugs.python.org/issue1602). It is possible to read and write Unicode in Windows console using C functions ReadConsoleW, WriteConsoleW instead of ReadConsole and WriteConsole. So one seems-to-be-working solution is to write your own stdout and stdin object, calling ReadConsoleW, WriteConsoleW via ctypes. For output this works, but for input there's a problem that Python interactive interpreter actually doesn't use sys.stdin for getting input (but calling input() function works) – see http://bugs.python.org/issue17620.

Many people say that there's a problem with Windows console. But you can actually type Unicode characters (if you have proper keyboard layout) with no problem. These are displayed with no problem. You can even run file called "∫.py" with some Unicode arguments and it is correctly run and arguments are correclty waiting in sys.argv strings.

Update: I have built a Python package to deal with these issues. See https://github.com/Drekin/win-unicode-console and https://pypi.python.org/pypi/win_unicode_console. Install by pip install win_unicode_console. It works at least for me on Python 3.4, Python 3.5, and Python 2.7.

这篇关于从cmd.exe运行时,Python的stdout中的Unicode输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆