1. 程式人生 > >python判斷檔案編碼型別

python判斷檔案編碼型別

import chardet

for file in ['decode.v', 'fen_ping.v', 'ji_shu.v', 'scanning.v', 'top_level.v']:
    file_path = 'F:/github_project/verilog_auto_check/' + file
    with open(file_path, 'rb') as f:
        data = f.read()
        print(file, end='  ')
        print('%s' % chardet.detect(data))

結果

decode.v  {'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}
fen_ping.v  {'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
ji_shu.v  {'encoding': 'ISO-8859-9', 'confidence': 0.5753719548238446, 'language': 'Turkish'}
scanning.v  {'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
top_level.v  {'encoding': 'ascii', 'confidence': 1.0, 'language': ''}

注意事項,讀取檔案的時候要以二進位制方式讀取,否則會報錯

TypeError: Expected object of type bytes or bytearray, got: <class 'str'>