Python简单检测文本类型的2种方法

Original 2017-01-16 09:22:59 505
abstract:本文实例讲述了Python简单检测文本类型的方法。分享给大家供大家参考,具体如下:1、根据文件头。#是否为带BOM头的UTF8文件 def IsUtf8BomFile(pathfile):   if b'\xef\xbb\xbf' == open(pathfile, mode='rb').read

本文实例讲述了Python简单检测文本类型的方法。分享给大家供大家参考,具体如下:

1、根据文件头。

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

2、用cchardet库。

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

  更多关于Python简单检测文本类型的2种方法请关注PHP中文网(m.sbmmt.com)其他文章!  


Release Notes

Popular Entries