chardet是一个Python库,用于检测文本文件的编码格式。
以下是一些基本的用法:
- 使用chardet.detect()函数检测文件编码
with open('example.txt', 'rb') as f:result = chardet.detect(f.read())
print(result['encoding'])
- 使用chardet.detect()函数检测字节串编码:
byte_str = b'\xe4\xbd\xa0\xe5\xa5\xbd'
result = chardet.detect(byte_str)
print(result['encoding'])
- 使用chardet.detect_all()函数检测多个文件编码
file_list = ['example1.txt', 'example2.txt']
results = []
for file in file_list:with open(file, 'rb') as f:result = chardet.detect(f.read())results.append(result)
print(results)
- 使用chardet.detect_stream()函数检测文件流编码
import iowith open('example.txt', 'rb') as f:stream = f.read()result = chardet.detect_stream(stream)
print(result['encoding'])