python 常用模块介绍
来源:互联网 发布:算法工程师jd 编辑:程序博客网 时间:2024/05/22 03:49
chardet
github: https://github.com/chardet/chardet
官网:http://pypi.python.org/pypi/chardet
chardet是一款用于字符串编码方式识别模块,这个模块是python的第三方库,所以需要进行安装。
安装
- 直接下载安装包方式安装
直接下载whl文件,放在python安装根目录\Lib\site-packages下,确保可以被python正确引用。 - 下载chardet源码方式,进行手动安装
sudo apt-get install pip #安装pipsudo apt-get install python-setuptools #安装setuptoolswget https://pypi.python.org/packages/fc/bb/a5768c230f9ddb03acc9ef3f0d4a3cf93462473795d18e9535498c8f929d/chardet-3.0.4.tar.gz#md5=7dd1ba7f9c77e32351b0a0cfacf4055c #下载chardet源码,也可以手动下载tar zxvf chardet-3.0.4.tar # 解压文件到当前目录cd chardet-3.0.4sudo python setup.py install # 进行安装
- 直接下载安装包方式安装
- 实例:
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'}
高级用法
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'}
阅读全文
0 0
- python常用模块介绍
- python 常用模块介绍
- Python常用模块介绍
- Python常用模块介绍
- python常用模块介绍
- Python 常用模块介绍
- python 常用模块介绍
- python常用模块介绍之一:string模块
- python基础31[常用模块介绍]
- python基础31[常用模块介绍]
- python基础31[常用模块介绍]
- python的functools模块常用方法介绍
- python常用模块介绍之二:copy模块
- python常用模块介绍之三:logging模块
- python常用模块介绍之四:getopt模块
- Python第五章__模块介绍,常用内置模块
- Python的os模块常用属性及方法介绍
- python常用模块介绍(转自itech's blog)
- Anaconda3 打开Navigator报错
- 【信息安全技术】DES算法的详细设计(简单实现)
- Java Activiti(8)--监听器
- JDBC操作数据库
- 训练总结 10.12
- python 常用模块介绍
- 集合list
- 顺序表应用7:最大子段和之分治递归法
- Quartz定时任务删除冗余操作日志
- 服务器发送事件SSE和web sockets实时通信
- web特殊記号
- 半数集问题(转)
- [agc011e]Increasing Numbers
- 栈和递归解决迷宫问题