欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python2.7在Windows下CMD编码为65001/utf-8时print报错[Errno 0]/[Errno 2]

程序员文章站 2022-03-25 19:11:42
使用python2.7处理unicode的字符串,环境变量已设置PYTHONIOENCODING为utf-8,cmd编码为utf-8时print unicode字符串会报错[Errno 0]或[Errno 2] 此时会报错,如果字符串只含ASCII字符就不会报错 经查这是windows实现C函数的问 ......

使用python2.7处理unicode的字符串,环境变量已设置pythonioencoding为utf-8,cmd编码为utf-8时print unicode字符串会报错[errno 0]或[errno 2]

#coding:utf-8
import os
os.system("chcp 65001")
a = u"你好こんにちは"
print a

此时会报错,如果字符串只含ascii字符就不会报错

 

经查这是windows实现c函数的问题

https://bugs.python.org/issue1602#msg148990

the underlying cause of python's write exceptions with cp65001 is:

the ansi c write() function as implemented by the windows console returns the number of _characters_ written rather than the number of _bytes_, which python reasonably interprets as a "short write error". it then consults errno, which gives the effectively random error message seen.

this can be bypassed by using os.write(sys.stdout.fileno(), utf8str), which will a) succeed and b) return a count <= len(utf8str).

with os.write() and an appropriate font, the windows console will correctly display a large number of characters.

possible workaround: clear errno before calling write, check for non-zero errno after. the vast majority of (non-python) applications never check the return value of write, so don't encounter this problem.

解决方法

方法1 使用win_unicode_console模块

1.安装

pip install win_unicode_console

2.使用

很简单,导入后设置开启就行

#coding:utf-8
import os
import win_unicode_console

win_unicode_console.enable()

os.system("chcp 65001")
a = u"你好こんにちは"
print a

方法2 不使用print

 根据issue的描述,可以用os.write(sys.stdout.fileno(), utf8str)的方式绕过

此时字符串不加u前缀,直接写入str类型

#coding:utf-8
import os
import sys
os.system("chcp 65001")
a = "你好こんにちは"
os.write(sys.stdout.fileno(), a)

偷懒方法

1.使用pycharm执行不会报错,推测pycharm自行修复了这个问题

2.只输出中文的话,那就不用utf8了,直接chcp 936然后输出a.encode("gbk","ignore")