本文實例講述了Python簡單檢測文本類型的方法。分享給大家供大家參考,具體如下:
1、根據文件頭。
1
2
3
4
5
|
#是否為帶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 } |
希望本文所述對大家Python程序設計有所幫助。