python chardet
来源:互联网 发布:go vs python 编辑:程序博客网 时间:2024/05/24 05:27
python 模块 chardet下载及介绍
在处理字符串时,常常会遇到不知道字符串是何种编码,如果不知道字符串的编码就不能将字符串转换成需要的编码。面对多种不同编码的输入方式,是否会有一种有效的编码方式?chardet是一个非常优秀的编码识别模块。
chardet 是python的第三方库,需要下载和安装。下载的地址有:
1.推荐地址: http://download.csdn.net/download/aqwd2008/4256178
2.官方地址: http://pypi.python.org/pypi/chardet
如果采用源代码安装方法,有可能会提示缺少setuptools这个模块。因此这里我们采用另外一种更为方便的安装方法,不论你选择哪种安装包,将它解压得到其中的文件夹【chardet】将这个文件夹复制到【python安装根目录\Lib\site-packages】下,确保这个位置可以被python引用到。如果不能被引用到请加入环境变量。
在安装完chardet模块,我就可以使用它了,来看一段示例代码。
- import chardet
- import urllib
- #可根据需要,选择不同的数据
- TestData = urllib.urlopen('http://www.baidu.com/').read()
- print chardet.detect(TestData)
- 运行结果:
- {'confidence': 0.99, 'encoding': 'GB2312'}
运行结果表示有99%的概率认为这段代码是GB2312编码方式。
另外一个相对高级的应用。
- import urllib
- from chardet.universaldetector import UniversalDetector
- usock = urllib.urlopen('http://www.baidu.com/')
- #创建一个检测对象
- detector = UniversalDetector()
- for line in usock.readlines():
- #分块进行测试,直到达到阈值
- detector.feed(line)
- if detector.done: break
- #关闭检测对象
- detector.close()
- usock.close()
- #输出检测结果
- print detector.result
- 运行结果:
- {'confidence': 0.99, 'encoding': 'GB2312'}
应用背景,如果要对一个大文件进行编码识别,使用这种高级的方法,可以只读一部,去判别编码方式从而提高检测速度。
来自:http://blog.csdn.net/aqwd2008/article/details/7506007
0 0
- python chardet
- python 模块 chardet
- Python chardet模块
- Python的chardet
- Python 插件杂谈 (1) ---- chardet
- Python字符编码检测 -- chardet
- windows下python chardet 安装
- Python chardet 字符编码判断
- python 中使用 chardet 模块
- Python下chardet的安装
- python-字符编码库chardet
- Python chardet 字符编码判断
- python编码检测模块chardet
- Python的chardet模块安装
- python chardet模块查看编码
- Win7 python安装pyquery chardet
- 探测python中字符集模块chardet
- windows下,安装python的chardet办法
- Swift学习笔记之闭包
- 常用.bash_profile配置
- OC加强第一天--内存管理MRC、@property参数
- ios第三方库类
- 浅谈协议和代理的使用
- python chardet
- 二分查找
- qt http 下载文件
- JAVA 设计模式-单例模式
- HDU 1287 破译密码
- JVM调优参数参考文章——择其所需(精)
- J - 水果
- css 内边距 外边距 边框
- oc中常用的编程解释积累(不断更新。。。)