python chardet简单应用
2015-06-14 16:24
821 查看
python的字符串编码识别模块(第三方库):
官方地址:http://pypi.python.org/pypi/chardet
运行结果表示有99%的概率认为这段代码是GB2312编码方式。
应用背景,如果要对一个大文件进行编码识别,使用这种高级的方法,可以只读一部,去判别编码方式从而提高检测速度。如果希望使用一个检测对象检测多个数据,在每次检测完,一定要运行一下detector.reset()。清除之前的数据。
官方地址:
importchardet
importurllib
#可根据需要,选择不同的数据
TestData=urllib.urlopen('http://www.baidu.com/').read()
printchardet.detect(TestData)
#运行结果:
#{'confidence':0.99,'encoding':'GB2312'}
运行结果表示有99%的概率认为这段代码是GB2312编码方式。
importurllib
fromchardet.universaldetectorimportUniversalDetector
usock=urllib.urlopen('http://www.baidu.com/')
#创建一个检测对象
detector=UniversalDetector()
forlineinusock.readlines():
#分块进行测试,直到达到阈值
detector.feed(line)
ifdetector.done:break
#关闭检测对象
detector.close()
usock.close()
#输出检测结果
printdetector.result
#运行结果:
#{'confidence':0.99,'encoding':'GB2312'}
应用背景,如果要对一个大文件进行编码识别,使用这种高级的方法,可以只读一部,去判别编码方式从而提高检测速度。如果希望使用一个检测对象检测多个数据,在每次检测完,一定要运行一下detector.reset()。清除之前的数据。
相关文章推荐
- Python time datetime常用时间处理方法
- Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
- python开发微信公众平台 BAE
- python中if __name = '__main__',#!/usr/bin/python 解析
- Python抓取给定URL网页里的图片
- Scrapy练习爬知乎第一辑
- 我为什么从 Python 转向 Go?
- Django数据库操作(python)
- Python之PyQT4的使用技巧集锦
- Simulate Blackjack Game - Card Generator
- Simulate Blackjack Game - Card Generator
- 搭建VTK+ITK+Python系统
- Python遍历文件夹,删除空文件夹,过滤特定大小和格式的文件
- 人人都懂的编程课(Python)
- 人人都懂的编程课(Python)
- Python 各种IDE比较
- python RSA签名
- leetcode Excel Sheet Column Number python
- leetcode Excel Sheet Column Title python
- leetcode Largest Number python