当前位置: 首页 > news >正文

真涨脸,我用 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真棒!

http://www.lryc.cn/news/26716.html

相关文章:

  • MySQL学习笔记(1.操作数据库与数据的SQL)
  • C++——特殊类设计
  • Scratch少儿编程案例-植物大战僵尸-趣味角色版
  • Vue的路由守卫
  • 【算法】151. 反转字符串中的单词
  • Azure AI基础到实战(C#2022)-认知服务(2)
  • 并发就一定快吗?答:肯定不是啊
  • 前端的学习路线和方法
  • 用C语言写一个自己的shell-Part Ⅱ--execute commands
  • 案例实践|运营腾讯游戏,Proxima Beta 使用 Apache Pulsar 升级团队协作与数据治理...
  • Hudi的7种索引
  • Linux内核(十三)系统软中断 software
  • Linux -- 查看进程 PS 命令 详解
  • C2科一考试道路通行规定
  • 进程概念(详细版)
  • 学习大数据应该掌握哪些技能
  • 【spring】Spring Data --Spring Data JPA
  • mysql数据库之视图
  • 数据库事务详解
  • Nessus: 漏洞扫描器-网络取证工具
  • 操作系统实战45讲之现代计算机组成
  • Simple Baselines for Image Restoration
  • Python数据可视化:局部整体图表可视化(基础篇—6)
  • CSDN新星计划新玩法、年度勋章挑战赛开启
  • Docker之部署Mysql
  • 基于C/C++获取电脑网卡的IP地址信息
  • 28相似矩阵和若尔当标准型
  • springboot操作MongoDB
  • 1月奶粉电商销售数据榜单:销售额约20亿,高端化趋势明显
  • 跨境数据传输是日常业务中经常且至关重要的组成部分