|
楼主 |
发表于 2024-9-19 23:11
|
显示全部楼层
- import numpy as np
- import pandas as pd
- import os
- import re
- # 导入库
- lujin = r'D:\python学习'
- df_empty = pd.DataFrame(columns=['班级', '姓名', '语文', '数学', '英语'])
- for parents, dirnames, filenames in os.walk(lujin):
- for filename in filenames:
- df = pd.read_excel(os.path.join(parents, filename))
- # 用正则提取日期
- pattern = r'(\d{4}-\d{1,2}-\d{1,2})'
- match = re.search(pattern, filename)
- substring = match.group(0)
- # df['日期'] = substring
- df.insert(0, '日期', substring)
- df_empty = df_empty.append(df, ignore_index=True)
- print(df_empty) # 输出合并表
- # df_empty.to_excel('分类汇总.xlsx',sheet_name='sheet1',index=False)
- with pd.ExcelWriter(lujin + '\\' + '合并.xlsx') as writer:
- df_empty.to_excel(writer, sheet_name='sheet1', index=False)
- # 合并表写入sheet1
复制代码 |
|