代码
import pandas as pd
import os
path = r'C:\Users\BXDM\Desktop\test'
outfile = r'C:\Users\BXDM\Desktop\test\out.csv'
def collect(path):
'''
合并函数
input(str(path)) : 输入 xlsx 文件所在的路径
output(DataFrame) : 输出合并后的 DataFrame
'''
files = os.listdir(path)
datas = pd.DataFrame()
for i in range(len(files)):
xlsx = pd.ExcelFile(path + '\\' + files[i])
data = pd.read_excel(xlsx)
froms = []
for j in range(len(data)):
froms.append(files[i])
data.insert(len(data.columns), 'Froms', froms)
datas = datas.append(data)
print('-----> ' + files[i])
return(datas)
data = collect(path)
data.to_csv(outfile, index = False, encoding = 'utf_8_sig')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ouy7ypKe-1636985771614)(…/…/…/AppData/Roaming/Typora/typora-user-images/image-20211115204531214.png)]
效果:
果:
[外链图片转存中…(img-dM3qY6xX-1636985771615)]
|