Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4446955
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: Python/Ruby

2012-04-26 20:42:48

文章来源:http://blog.csdn.net/yatere/article/details/6654860

文档:


其实还有一个:


isinstance(s, str) 用来判断是否为一般字符串

isinstance(s, unicode) 用来判断是否为unicode

下面才是正文:

Basic usage

The easiest way to use the Universal Encoding Detector library is with the detect function.


Example: Using the detect function

The detect function takes one argument, a non-Unicode string. It returns a dictionary containing the auto-detected character encoding and a confidence level from 0 to 1.

  1. import urllib  
  2. rawdata = urllib.urlopen('').read()  
  3. mport chardet  
  4. print (chardet.detect(rawdata))  
  5. #{'encoding': 'EUC-JP', 'confidence': 0.99}  



Advanced usage

If you’re dealing with a large amount of text, you can call the Universal Encoding Detector library incrementally, and it will stop as soon as it is confident enough to report its results.

Create a UniversalDetector object, then call its feed method repeatedly with each block of text. If the detector reaches a minimum threshold of confidence, it will set detector.done to True.

Once you’ve exhausted the source text, call detector.close(), which will do some final calculations in case the detector didn’t hit its minimum confidence threshold earlier. Then detector.result will be a dictionary containing the auto-detected character encoding and confidence level (the same as ).


Example: Detecting encoding incrementally
  1. import urllib  
  2. from chardet.universaldetector import UniversalDetector  
  3.   
  4. usock = urllib.urlopen('')  
  5. detector = UniversalDetector()  
  6. for line in usock.readlines():  
  7.     detector.feed(line)  
  8.     if detector.done: break  
  9. detector.close()  
  10. usock.close()  
  11. print (detector.result)  
  12.   
  13. #{'encoding': 'EUC-JP', 'confidence': 0.99}  


If you want to detect the encoding of multiple texts (such as separate files), you can re-use a single UniversalDetector object. Just call detector.reset() at the start of each file, call detector.feed as many times as you like, and then call detector.close() and check the detector.result dictionary for the file’s results.


Example: Detecting encodings of multiple files

  1. import glob  
  2. from chardet.universaldetector import UniversalDetector  
  3.   
  4. detector = UniversalDetector()  
  5. for filename in glob.glob('*.xml'):  
  6.     print (filename.ljust(60))  
  7.     detector.reset()  
  8.     for line in file(filename, 'rb'):  
  9.         detector.feed(line)  
  10.         if detector.done: break  
  11.     detector.close()  
  12.     print (detector.result) 

阅读(1963) | 评论(0) | 转发(0) |
0

上一篇:Python deque用法介绍

下一篇:Closures in Python

给主人留下些什么吧!~~