Python2.7在Windows下CMD编码为65001/utf-8时print报错[Errno 0]/[Errno 2]
使用python2.7处理unicode的字符串,环境变量已设置PYTHONIOENCODING为utf-8,cmd编码为utf-8时print unicode字符串会报错[Errno 0]或[Errno 2](python3.6环境下未出现此问题)
#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")
Python2.7在Windows下CMD编码为65001/utf-8时print报错[Errno 0]/[Errno 2]的更多相关文章
- windows下cmd命令行显示UTF8字符设置(CHCP命令)
本文由 www.169it.com 收集整理 在中文Windows系统中,如果一个文本文件是UTF-8编码的,那么在CMD.exe命令行窗口(所谓的DOS窗口)中不能正确显示文件中的内容.在默认情况下 ...
- windows下cmd时复制dos中的内容 错误信息等
16:28 2015/11/23小发现 windows下cmd时复制dos中的内容,错误信息等:鼠标右键选择标记,然后ctrl c 即可.
- windows下cmd常用
windows下cmd常用 shutdown -s -t 2------2秒后关机 加上-f选项意思是强制执行 shutdown -r -t 2------2秒后重启 加上-f选项意思是强制执行 lo ...
- windows下cmd中命令操作
windows下cmd中命令: cls清空 上下箭头进行命令历史命令切换 ------------------------------------------------------------- ...
- windows下cmd清屏命令cls
windows下cmd清屏命令cls
- windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决 一.发现问题 由于tomcat内存溢出,在wind ...
- Winform下CefSharp的引用、配置、实例与报错排除(源码)
Winform下CefSharp的引用.配置.实例与报错排除 本文详细介绍了CefSharp在vs2013..net4.0环境下,创建Winfrom项目.引用CefSharp的方法,演示了winfro ...
- linux下启动dbca或netmgr类的图形界面报错解决
linux下启动dbca或netmgr类的图形界面报错解决 Xlib: connection to ":0.0" refused by server Xlib: No pro ...
- Ansible 脚本运行一次后,再次运行时出现报错情况,原因:ansible script 的格式不对,应改成Unix编码
Ansible 脚本运行一次后,再次运行时出现报错情况,原因:ansible script 的格式不对,应改成Unix编码 find . -name "*" | xargs do ...
随机推荐
- winform listbox 使用DrawMode使用OwnerDrawVarialbe或OwnerDrawFixed无水平滚动条
因为需要使用DrawMode自行DrawItem,所以需要将DrawMode设置为OwnerDrawVarialbe或OwnerDrawFixed模式,代码如下: private void listB ...
- LeetCode题解之Rotate Array
1.题目描述 2.代码 void rotate(vector<int>& nums, int k) { ) return ; && (k / nums.size() ...
- linux/OSX中“DD”命令制作ISO镜像操作系统安装U盘
linux或者OS X系统中,使用“dd”命令可以直接在终端命令行模式下,制作ISO镜像的系统安装盘. 一.linux系统以centOS7为例. sudo dd if=镜像路径 of=USB设备路径 ...
- js获取元素显示隐藏的当前状态
js获取元素显示隐藏的当前状态 // CSS var display = $("."+cls).css("display"); if(display == &q ...
- [SQL Server]SQL行转列
SELECT * FROM (select ActionTargetType+actiontype as TypeResult, COUNT(RowGuid) as Number from BanJi ...
- Windows下Git使用报错:warning:LF will be replaced by CRLF in ××××.××
Windows下Git使用报错: warning:LF will be replaced by CRLF in ××××.××(文件名) The file will have its original ...
- QuickBI助你成为分析师-数据建模(二)
摘要: 数据集编辑功能界面介绍以及常见问题总结. 在数据集编辑界面可以进行数据建模来更好的展示数据,创建数据集默认将数值类型字段作为度量,日期.字符串等类型作为维度,度量可以根据维度分组展示.下面来介 ...
- Java通过Shell执行Sqoop命令没日志的问题
修改执行部分的代码,改成用InputStream.read(byte[])的方法从流中读取数据 package com.example.demo.utils; import java.io.*; pu ...
- Hadoop HBase概念学习系列之RowKey设计(二十九)
HBase里的RowKey设计,分为随机查询的RowKey设计和连续查询的RowKey设计.
- Hadoop HBase概念学习系列之HFile(二十)
HFile文件是不定长的. HFile里才是想要的真正数据,实际存储的位置,是在HDFS上.