import chardet
def detect_file_encoding(filename):
with open(filename, 'rb') as file:
raw_data = file.read()
result = chardet.detect(raw_data)
print(f"检测结果: {result}")
return result['encoding']
# 使用示例
encoding = detect_file_encoding('your_file.txt')
print(f"文件编码: {encoding}")