本文实例讲述了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程序设计有所帮助。