xlsx文件转csv文件注意事项:python默认输出编码为‘utf-8’,excel默认读取文件编码为'gbk',写入csv时,需要指定编码方式:encoding='gbk'。
import pandas as pd
import os
import time
def save_as_csv():
time_start=time.time()
data_path=str(input('请输入要转换的文件路径->|'))
print('='*90)
for dirpath,dirname,filenames in os.walk(data_path):
for fname in filenames:
file_path=os.path.join(dirpath,fname)
if fname.endswith('.xlsx'):
floder_name,ex=os.path.splitext(file_path)
new_file_path=floder_name+'.csv'
df=pd.read_excel(file_path)
try:
df.to_csv(new_file_path,sep=',',encoding='gbk')
os.remove(file_path)
except:
df.to_csv(new_file_path,sep=',',encoding='utf-8')
os.remove(file_path)
else:
print(f'非xlsx文件->|{file_path}')
time_end=time.time()
time_sum=round((time_end-time_start),2)
print(f'文件转换完成,程序运行->| {time_sum}s')
print('='*90)
save_as_csv()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)