Python编码爬坑指南(必看)


Posted in Python onJune 10, 2016

自己最近有在学习python,这实在是一门非常短小精悍的语言,很喜欢这种语言精悍背后又有强大函数库支撑的语言。可是刚接触不久就遇到了让人头疼的关于编码的问题,在网上查了很多资料现在在这里做一番总结,权当一个记录也为后来的兄弟姐妹们服务,如果可以让您少走一些弯路本人将倍感荣幸。

先来描述下现象吧:

import os
for i in os.listdir("E:\Torchlight II"):
  print i

代码很简单我们使用os的listdir函数遍历了E:\Torchlight II这个目录(Torchlight ?! :)),由于这个目录下有些文件是以中文命名的,所以在最后print结果时出现了乱码,像这样:

Python编码爬坑指南(必看)

那么问题出在哪儿呢? 别急,我们一点一点来分析它。

这里这里我们几乎能够肯定的知道问题是出在:

This means that the python console app can't write the given character to the console's encoding.
More specifically, the python console app created a _io.TextIOWrapperd instance with an encoding that cannot represent the given character.
sys.stdout --> _io.TextIOWrapperd --> (your console)

看到这里不知你是否与我想的一样,能不能去设置console的编码,将其设置为能够理解中文字符的编码不就可以正常的显示出中文了吗?等等,让我们在多Google一会儿,

Python determines the encoding of stdout and stderr based on the value of the LC_CTYPE variable, but only if the stdout is a tty. So if I just output to the terminal, LC_CTYPE (or LC_ALL) define the encoding. However, when the output is piped to a file or to a different process, the encoding is not defined, and defaults to 7-bit ASCII.

更详细的说明如下:

1). When Python finds its output attached to a terminal, it sets the sys.stdout.encoding attribute to the terminal's encoding. The print statement's handler will automatically encode unicode arguments into str output.
2). When Python does not detect the desired character set of the output, it sets sys.stdout.encoding to None, and print will invoke the "ascii" codec.

嚯嚯,看来刚才的想法是可行的只是不太优雅罢了,因为我们得去修改系统的设置。事实上上面的论述是基于linux环境的,在linux下可能需要我们去更改某个环境变量的值(LC_CTYPE or LANG);如果我们是在windows下面的话,console的编码设置是跟操作系统的区域设置相关的。比如在中文的win7环境下,console默认的编码就是GBK(cp936)。你可以试试下面的代码:

import locale
print locale.getdefaultlocale()[1]

console的编码不好设置了那能否对stdout.out.encoding进行设置以达到我们的目的呢?很遗憾,答案是否定的,这家伙压根就是只读的:

Python编码爬坑指南(必看)

没有办法了么?不会,其实我们离成功已经很近了,来,根据上面检索到的那些资料分析整理下看看我们现在掌握到的情况都有哪些:

 

1). console不能正常显示中文,console的编码是由操作系统决定的(windows环境下);
 2). 我的操作系统是win7中文版(GBK),enc = locale.getdefaultlocale()[1];
 3). console的编码决定了sys.stdout.encoding的取值,sys.stdout.encoding = utf-8;
 4). 从操作系统枚举目录(E:\Torchlight II)列表返回的字符串也是GBK编码

 是不是已经看出问题来了。最上面截图中那么奇奇怪怪的问号尖角符号就是因为字符串本身是按照gbk进行编码的,但是由于sys.stdout.encoding = utf-8,导致print会按照utf-8对input的数据进行encode从而转换为unicode字符。这,当然错误了。原因已经清楚了,来改改代码吧:

import os
for i in os.listdir("E:\Torchlight II"):
  print i.decode('gbk')

在代码中我们手动告诉了python对读入的字符串按章gbk编码来进行解码,而这一个动作之后数据已经是标准的unicode字符了,可以放心的交给print去打印输出了(即使这会儿sys.stdout.encoding = utf-8):

Python编码爬坑指南(必看)

 ps:

实际在google中还查到过很多相关的类似编码的问题,比如这里的,还有这里的。虽然问题的样子千变万化并且解决方式多种多样甚至是python自己的特定解决方式,比如这里。但这些问题本质都是一样的都是关于字符的编码和解码,搞清楚了其中的本质所有问题都能够迎刃而解。

以上这篇Python编码爬坑指南(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
Aug 22 Python
windows下python连接oracle数据库
Jun 07 Python
python实现二叉查找树实例代码
Feb 08 Python
Python玩转PDF的各种骚操作
May 06 Python
python GUI图形化编程wxpython的使用
Jul 19 Python
简单的Python调度器Schedule详解
Aug 30 Python
python中@property和property函数常见使用方法示例
Oct 21 Python
Django REST Swagger实现指定api参数
Jul 07 Python
python palywright库基本使用
Jan 21 Python
手把手教你用Django执行原生SQL的方法
Feb 18 Python
python实现自动清理文件夹旧文件
May 10 Python
Django实现聊天机器人
May 31 Python
浅析Python中的for 循环
Jun 09 #Python
Python多层嵌套list的递归处理方法(推荐)
Jun 08 #Python
Python-嵌套列表list的全面解析
Jun 08 #Python
PYTHON压平嵌套列表的简单实现
Jun 08 #Python
Python用Bottle轻量级框架进行Web开发
Jun 08 #Python
浅谈Python数据类型之间的转换
Jun 08 #Python
浅谈python 四种数值类型(int,long,float,complex)
Jun 08 #Python
You might like
php循环输出数据库内容的代码
2008/05/24 PHP
PHP求小于1000的所有水仙花数的代码
2012/01/10 PHP
php数组保存文本与文本反编成数组实例
2014/11/13 PHP
Yii开启片段缓存的方法
2016/03/28 PHP
JS实现静止元素自动移动示例
2014/04/14 Javascript
jQuery模拟点击A标记示例参考
2014/04/17 Javascript
JavaScript学习笔记之定时器
2015/01/22 Javascript
js代码实现点击按钮出现60秒倒计时
2021/01/28 Javascript
Bootstrap每天必学之日期控制
2016/03/07 Javascript
纯JS实现可拖拽表单的简单实例
2016/09/02 Javascript
基于jQuery实现照片墙自动播放特效
2017/01/12 Javascript
JavaScript验证知识整理
2017/03/24 Javascript
vue通过style或者class改变样式的实例代码
2018/10/30 Javascript
es6数据变更同步到视图层的方法
2019/03/04 Javascript
vue 通过base64实现图片下载功能
2020/12/19 Vue.js
python实现多线程的方式及多条命令并发执行
2016/06/07 Python
Django使用httpresponse返回用户头像实例代码
2018/01/26 Python
pygame实现雷电游戏雏形开发
2018/11/20 Python
python 内置模块详解
2019/01/01 Python
Jupyter notebook在mac:linux上的配置和远程访问的方法
2019/01/14 Python
Python实现的大数据分析操作系统日志功能示例
2019/02/11 Python
numpy.random模块用法总结
2019/05/27 Python
Python倒排索引之查找包含某主题或单词的文件
2019/11/13 Python
韩国CJ食品专卖网:CJonmart
2016/09/11 全球购物
英国复古皮包品牌:Beara Beara
2018/07/18 全球购物
澳大利亚先进的皮肤和激光诊所购物网站:Soho Skincare
2018/10/15 全球购物
《莫泊桑拜师》教学反思
2014/04/23 职场文书
法制宣传标语集锦
2014/06/25 职场文书
乡镇综治宣传月活动总结
2014/07/02 职场文书
2015年禁毒工作总结
2015/04/30 职场文书
《月球之谜》教学反思
2016/02/20 职场文书
幼儿园六一儿童节开幕词
2016/03/04 职场文书
教你如何用python开发一款数字推盘小游戏
2021/04/14 Python
python opencv人脸识别考勤系统的完整源码
2021/04/26 Python
教你利用python实现企业微信发送消息
2021/05/23 Python
Win10此设备不支持接收Miracast无法投影的解决方法
2022/07/07 数码科技