IDE:pycharm2021.1
代码会自动遍历参数pwd下的文件及子文件夹中的文件,将其自动拼接成一个文件,但是每个文件中的表头会保留,故需要去重。
因为xls文件不能保存65000+行数据,故保存选用.xlsx文件。
合并代码:
import os
import pandas as pd
pwd='C:\\Users\\caoao\\OneDrive\\xx数分\\1'
file_list=[]
dfs=[]
for root,dirs,files in os.walk(pwd):#root起始路径dirs路径下的文件夹files文件
for file in files:
if not file.endswith('.xls'):
continue#如果遇到其它文件类型的文件,则跳过。
file_path=os.path.join(root,file)
file_list.append(file_path)
dfg=pd.read_excel(file_path)
dfs.append(dfg)
df=pd.concat(dfs)
df.to_excel('C:\\Users\\caoao\\OneDrive\\xx数分\\1\\result.xlsx',index=False)
原始文件:
成果: