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 ...
随机推荐
- C/S模式下的打印方法
C/S模式使用润乾报表时有两种打印方法(都使用设计器授权) 1. 使用加密狗打印 这种方式需要使用加密狗,适用于客户端较少时 2. 使用api调用打印方法实现打印 ...
- mysql navicat 及命令行 创建、删除数据库
1.命令行创建数据库 create database mybatis default character set utf8 collate utf8_general_ci; drop database ...
- C语言图形编程
四.图形和图像函数(一) 像素函数 56. putpiel() 画像素点函数 57. getpixel()返回像素色函数(二) 直线和线型函数 58. line() 画线函数 ...
- 测试、集成等领域最好的Java工具
无论你是刚入门,还是进行了一段时间的开发,使用合适的工具编程都会让你事半功倍,它能够让你更快的编写代码,能够快速及时的为你识别出Bug,能够让你的代码质量更上一层楼. 如果你选择的编程语言是Java, ...
- SQLSERVER中KeyHashValue的作用(下)
SQLSERVER中KeyHashValue的作用(下) 昨天中午跟高文佳童鞋讨论了KeyHashValue的作用,到最后还是没有讨论出结果 昨天晚上德国的兄弟傅文伟做了一下实验,将实验结果交给我 感 ...
- C# .Net动态调用webService
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...
- [翻译] NSImage+HHTint - Tints grayscale images using CoreImage
NSImage+HHTint - Tints grayscale images using CoreImage https://github.com/gloubibou/NSImage-HHTint ...
- Request URL参数
登录跳转完整参考: http://www.cnblogs.com/dreamer-fish/p/5435274.html request.META.get('HTTP_REFERER', '/') # ...
- 工作中碰到的一个问题(cookie相关)
今天上线了一个API,6台机器做的集群.API的第一步是读取cookie,判断用户是否登录. 例如,线上服务器分别是 10.255.242.1 10.255.242.2 10.255.242.3 10 ...
- IOS的滑动菜单(Sliding Menu)的具体写法(附代码)
滑动菜单是一个很流行的IOS控件 先上效果图: 这里使用github的JTReveal框架来开发,链接是https://github.com/agassiyzh/JTRevealSide ...