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 ...
随机推荐
- java实现Kafka生产者示例
使用java实现Kafka的生产者 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- CSS 颜色 字体 背景 文本 边框 列表 display属性
1 颜色属性 <div style="color:blueviolet">ppppp</div> <div style="color:#f ...
- clr_zmq Vs2010版本
.net的消息队列很方便的一个库. 在github上的主版本虽然也支持fw4.0,但是必须使用vs2012以上进行编译. 这样就依赖vcredist运行时. 因为win7 sp1以下版本,无法安装vc ...
- 【转】数据分析与处理之二(Leveldb 实现原理)
郑重声明:本篇博客是自己学习 Leveldb 实现原理时参考了郎格科技系列博客整理的,原文地址:http://www.samecity.com/blog/Index.asp?SortID=12,只是为 ...
- Oracle EBS 查询用户职责
select b.user_name UserID, b.description User_Name, f.RESPONSIBILITY_NAME RESPONSIBILITY from FND_US ...
- [WinCE | VS2008 | Solution] VS2008 building WinCE projects taking a long time
1. Open C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CompactFramework.Common.targets 2. Find pa ...
- OSCache-缓存对象
在实际应用中除了JSP标签库,还可以使用OSCache提供的Java API.下面我来介绍一个实用的Java类,使用GeneralCacheAdministrator来建立,刷新和管理缓存. Gene ...
- Linux 正则表达式详解
正则表达式(REGULAR):为处理大量的字符串而定义的一套规则和方法,为了处理大量字符串而生 常见命令参数 基础正则表达式 . :有且只有任意一个字符(包括空格) * :重复前面任意0或者多个字符 ...
- 三元运算符 c = a if a>b else b
def my(a,b): c = a if a>b else b return c d = my(1,12)print(d)
- 关于安装AndroidStudio中遇见的问题
安装AndroidStudio: 最近,准备了解下Android,就买了本<第一行代码Android:第二版>, 最开始就是安装AndroidStudio,刚开始以为安装的挺容易的,结果… ...