安装python对应库
pip install chardet
检测文件编码
import chardet
# 检测文件编码
file_path = r'C:\Users\AA\Desktop\log.log' # 这里放文件和文件绝对路径
with open(file_path, 'rb') as f:
raw_data = f.read(100000) # 读取前10000个字节
result = chardet.detect(raw_data)
encoding = result['encoding']
confidence = result['confidence']
print(f"检测到的编码: {encoding}, 置信度: {confidence}")
使用检测到的编码读取文件
# 使用检测到的编码读取文件
detected_encoding = 'GB2312' # 替换为检测到的编码
output_file_path = r'C:\Users\AA\Desktop\log_fixed.txt' # 这里写输出的文件的路径
try:
with open(file_path, 'r', encoding=detected_encoding, errors='ignore') as f:
text = f.readlines()
with open(output_file_path, 'w', encoding='utf-8') as f:
f.writelines(text)
print("文件编码转换完成,已保存为 log_fixed.txt。")
except Exception as e:
print(f"处理过程中出现错误: {e}")
用jupyter运行结果如下