kiloyp 发表于 2016-3-30 14:58:34

Python检测文本类型

1、根据文件头。


1
2
3
4
5
6
#是否为带BOM头的UTF8文件
def IsUtf8BomFile(pathfile):
    if b'\xef\xbb\xbf' == open(pathfile, mode='rb').read(3)):
      return True
         
    return False




2、用cchardet库。

1
2
3
>>> import cchardet
>>> cchardet.detect(open(pathfile, 'rb').read())
{'encoding': 'UTF-8', 'confidence': 0.9900000095367432}





页: [1]
查看完整版本: Python检测文本类型