python判断文件编码类型
程序员文章站
2024-02-19 08:27:46
...
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'>