首页 > python编码检测原理以及chardet模块应用

python编码检测原理以及chardet模块应用

有时候需要先检测一个文件的编码,然后将其转化为另一种编码。这时候就会用到chardet(chardet是python的一个第三方库,是非常优秀的编码识别模块)

chardet有两种检测文件编码的方法:
一、

>>> import chardet
>>> f = open('songs.txt','r')
>>> result = chardet.detect(f.read())
>>> result
{'confidence': 0.99, 'encoding': 'utf-8'}

二、chardet comes with a command-line script which reports on the encodings of one or more files:

% chardetect.py somefile someotherfile
somefile: windows-1252 with confidence 0.5
someotherfile: ascii with confidence 1.0
def description_of(file, name='stdin'):
    """Return a string describing the probable encoding of a file."""
    u = UniversalDetector()
    for line in file:
        u.feed(line)
    u.close()
    result = u.result
    if result['encoding']:
        return '%s: %s with confidence %s' % (name,
                                              result['encoding'],
                                              result['confidence'])
    else:
        return '%s: no result' % name

猜测:第一种检测编码的方法可能类似于vim,从小的编码集合(比如说ascii)开始解析数据,计算解码错误率,错误率超过阈值,则换用更大的字符集合,直到得到一个可以容忍的解码结果。因此速率会慢,文件比较大的话此方法不是很合适。不知道是不是这样子的?

问题:

  1. 二的原理是什么,难道是每行每行地解析数据,然后看有没有解码错误?
  2. 用二的方法检测时,有时候检测小文本花费时间比大文本长很多(其中大文本只是简单的重复小文本中的内容)

二的原理是什么,难道是每行每行地解析数据,然后看有没有解码错误?

一、二都差不多。
第一个会一次性读取全部文件,可能会浪费点内存(chardet的探测会在搜集到足够数据之后停止,文件比较大时,就比较浪费);
第二个按行读取。

用二的方法检测时,有时候检测小文本花费时间比大文本长很多(其中大文本只是简单的重复小文本中的内容)

因为chardet是按照词频统计的,所以探测大量重复数据的时间比不重复数据的时间可能要小点。

【热门文章】
【热门文章】