赞
踩
import os import pandas as pd import chardet def convert_encoding(file_path, file_encoding): # try: # 读取文件 with open(file_path, 'rb') as f: data = f.read() # 检测文件编码方式 file_encoding = chardet.detect(data)['encoding'] print(file_encoding) # 重新编码文件 if file_encoding != 'utf-8': df = pd.read_csv(file_path, sep=None, encoding=file_encoding, engine='python') df.to_csv(file_path, sep=',', encoding='utf-8', index=False) # except Exception as e: # print('Error:', e) def encoding_transform(folder_path, file_ext): # 遍历文件夹下所有文件 for file_name in os.listdir(folder_path): file_path = os.path.join(folder_path, file_name) # 如果是csv文件 if os.path.isfile(file_path) and os.path.splitext(file_path)[1] == '.' + file_ext: convert_encoding(file_path, 'utf-8') encoding_transform(r'D:\csv_test', 'csv')
在用office打开时,可能会出现乱码的情况,将encoding='utf-8'变成encoding='utf_8_sig'即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。