如何使用python在chcp 65001下正确打印cmd.exe中的宽字符?

时间:2018-02-24 07:52:32

标签: python unicode cmd

这看起来像Python问题,因为使用python print或使用cmd echo调用子进程,我可以得到不同的结果。(参见下面的示例)

问题是如何在chcp65001下正确打印cmd.exe中的宽字符? 是否有python打印方法中的选项或我需要做的任何特殊方法来适应Windows环境。

OS Win7 64bit。

==GOOD==

C:\>python -c "import subprocess; subprocess.run('cmd.exe /c echo 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊', shell=True)"
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

==BAD==
C:\> python -c "print('啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊')"
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
�啊啊啊啊啊啊啊啊啊啊啊
��啊啊啊啊啊啊
�啊啊啊
�啊

C:\>chcp
Active code page: 65001

C:\>python -V
Python 3.5.2

enter image description here

已经有一个Perl基础修复:

C:\>python -c "print('啊啊啊啊啊啊啊啊啊啊啊啊啊')" | perl -Mutf8 -ne "BEGIN{binmode(STDIN,':unix:encoding(utf8):crlf');binmode(STDOUT, ':unix:encoding(utf8):crlf');}print"
啊啊啊啊啊啊啊啊啊啊啊啊啊

C:\>chcp
Active code page: 65001

Why am I getting the last octet repeated when my Perl program outputs a UTF-8 encoded string in cmd.exe?

拥有Python解决方案会很好 enter image description here

0 个答案:

没有答案