status

草稿

清风 & liz; 100%

PCS202 chardet

概述

chardet是一个开源的字符编码自动检测模块,是基于Python实现的.读者可以在chardet的网站上下载这个模块并根据源码包中相关说明进行安装.

使用

检测字符编码

利用chardet来自动检测字符编码的最简单方式是使用dectect(),它以一个非unicode字符串作为参数,返回一个由被检测字符串的编码方式及其可信度数值(介于0~1之间)组成的字典数据结构.下面的例子中,首先使用urllib.urlopen方法打开百度首页并读取其内容作为被检测数据,然后使用chardet.detect检测并返回结果:

>>> import urllib
>>> rawdata = urllib.urlopen('http://www.baidu.com').read()
>>> import chardet
>>> chardet.detect(rawdata)
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}

可以看到,检测出该网页编码方式为GB2312的可信度是0.98999999999999999,一个非常高的值.

渐进检测字符编码

对于大量的文本,提供了可以渐进检测字符编码的相关方法并且在满足可信度后会自动停止检测.具体是通过UniversalDetector类的feed方法不断检测每个文本块,当达到最小可信度阈值时,就标记UniversalDetector类的done值为True,表示完成检测. 已经读取完待检测文本之后若调用UniversalDetector类的close方法,它会在没有达到最小可信度的情况下再做一些计算,以便返回最大程度准确的值,该值是和chardet.detect函式一样的字典结构.

   1 import urllib
   2 from chardet.universaldetector import UniversalDetector
   3 
   4 usock = urllib.urlopen('http://www.baidu.com')
   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

这个例子同样先打开百度首页,接着创建了一个UniversalDetector对象detector,在接下来的for循环中,读取数据的同时检测该数据,当done值为True时表示检测完成退出for循环,最后关闭数据流并打印结果:

~$ python pcs-202-1.py 
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}

如果想对多个独立文本进行编码检测,可以重复使用同一个UniversalDetector对象,只要在每个文本开始检测前调用reset()以表明接下来读取的是与之前文本独立的字符串,然后就可以类似于上述例子中的,去调用feed(),最后调用close()结束,最终的检测结果放在result中.

   1 import glob
   2 from chardet.universaldetector import UniversalDetector
   3 
   4 detector = UniversalDetector()
   5 for filename in glob.glob('*.py'):
   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

其运行结果如下:

~$ python pcs-202-2.py 
pcs-202-1.py                                                 {'confidence': 1.0, 'encoding': 'ascii'}
pcs-202-2.py                                                 {'confidence': 1.0, 'encoding': 'ascii'}