真涨脸,我用 Python 为朋友自动化整理表格

今天,在工作的时候,我的美女同事问我有没有办法自动生成一个这样的表格:

图片

第一列是院校+科目,第二列是年份,第三列是数量。

这张表格是基于这一文件夹填充的,之前要一个文件夹一个文件夹打开然后手动填写年份和数量

图片

手动整理需要耗费较长时间,于是我便开发了一个 Python 程序用来自动生成归纳表格

利用正则表达式+OS库+openpyxl生成真题年份归纳表格

技术提升

技术要学会分享、交流,不建议闭门造车。一个人走的很快、一堆人可以走的更远。

本文来自技术群粉丝的分享、推荐,资料、代码、数据、技术交流提升,均可加交流群获取,群友已超过2000人,添加时切记的备注方式为:来源+兴趣方向,方便找到志同道合的朋友。

方式①、添加微信号:pythoner666,备注:来自 CSDN + python
方式②、微信搜索公众号:Python学习与数据挖掘,后台回复:加群

原理

第一步,遍历文件夹下的所有文件和子文件夹的名称,并获取子文件夹下的文件的年份信息和数量信息

第二步,将年份信息进行格式化,连续的年份取最小值和最大值,并用“-”连接,单独的年份单独提取出,并用顿号连接

第三步,写入数据到Excel中

目标实现

遍历文件,新建数据存放的List

path=os.getcwd()
file_list=list(os.walk(path))
infomation=[]
yearList=[]

获取信息

 if '/' in path:
  infomation.append(file_list[i][0].replace(path+'/',''))
 elif '\\' in path:
  infomation.append(file_list[i][0].replace(path+'\\',''))
 totalNum=len(file_list[i][2])
 for j in range (0,len(file_list[i][2])):
  year=re.findall(r'\d{4}',file_list[i][2][j])
  yearList.append(int(year[0]))
 yearList.sort()

年份信息格式化

for i in range(len(yearList)):
  if not res:
   res.append([yearList[i]])
  elif yearList[i-1]+1==yearList[i]:
   res[-1].append(yearList[i])
  else:
   res.append([yearList[i]])
 y=[]
 for m in range (0,len(res)):
  if(max(res[m])==min(res[m])):
   y.append(str(max(res[m])))
  else:
   y.append(str(min(res[m]))+'-'+str(max(res[m])))
 yearInfo="、".join(y)

保存数据并输出到Excel中

infomation.append(yearInfo)
 infomation.append(totalNum)
 print(infomation)
 ws.append(infomation)
 wb.save('表格.xlsx')
 infomation=[]
 yearList=[]

最终的完整代码如下

import os
import re
from openpyxl import load_workbook
wb=load_workbook('表格.xlsx')
ws=wb.active
path=os.getcwd()
file_list=list(os.walk(path))
infomation=[]
yearList=[]
for i in range (1,len(file_list)):
 if '/' in path:
  infomation.append(file_list[i][0].replace(path+'/',''))
 elif '\\' in path:
  infomation.append(file_list[i][0].replace(path+'\\',''))
 totalNum=len(file_list[i][2])
 for j in range (0,len(file_list[i][2])):
  year=re.findall(r'\d{4}',file_list[i][2][j])
  yearList.append(int(year[0]))
 yearList.sort()
 res=[]
 for i in range(len(yearList)):
  if not res:
   res.append([yearList[i]])
  elif yearList[i-1]+1==yearList[i]:
   res[-1].append(yearList[i])
  else:
   res.append([yearList[i]])
 y=[]
 for m in range (0,len(res)):
  if(max(res[m])==min(res[m])):
   y.append(str(max(res[m])))
  else:
   y.append(str(min(res[m]))+'-'+str(max(res[m])))
 yearInfo="、".join(y)
 infomation.append(yearInfo)
 infomation.append(totalNum)
 print(infomation)
 ws.append(infomation)
 wb.save('表格.xlsx')
 infomation=[]
 yearList=[]

运行效果

图片

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v4QFW8CR-1677769009623)(null)]

图片
好啦,程序不复杂,不过却大大提高了工作效率,不得不说,Python真棒!

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值