中国网络水泥 发表于 2018-8-12 14:12:57

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}
  相关阅读:
  1、u'\ufeff' in Python string (里面有几种文本的头信息)
  2、wxMEdit 跨平台的文本/十六进制编辑器
  3、收集各种文件类型的文件头信息
  4、根据文件头数据判断文件类型
  *** walker ***
页: [1]
查看完整版本: Python检测文本类型