个人网站建设完整教程,快速排名优化,有哪些图片设计网站有哪些问题,东莞市网站建设公司今天#xff0c;在工作的时候#xff0c;我的美女同事问我有没有办法自动生成一个这样的表格#xff1a; 第一列是院校科目#xff0c;第二列是年份#xff0c;第三列是数量。
这张表格是基于这一文件夹填充的#xff0c;之前要一个文件夹一个文件夹打开然后手动填写年份…今天在工作的时候我的美女同事问我有没有办法自动生成一个这样的表格 第一列是院校科目第二列是年份第三列是数量。
这张表格是基于这一文件夹填充的之前要一个文件夹一个文件夹打开然后手动填写年份和数量 手动整理需要耗费较长时间于是我便开发了一个 Python 程序用来自动生成归纳表格
利用正则表达式OS库openpyxl生成真题年份归纳表格
技术提升
技术要学会分享、交流不建议闭门造车。一个人走的很快、一堆人可以走的更远。
本文来自技术群粉丝的分享、推荐资料、代码、数据、技术交流提升均可加交流群获取群友已超过2000人添加时切记的备注方式为来源兴趣方向方便找到志同道合的朋友。 方式①、添加微信号pythoner666备注来自 CSDN python 方式②、微信搜索公众号Python学习与数据挖掘后台回复加群 原理
第一步遍历文件夹下的所有文件和子文件夹的名称并获取子文件夹下的文件的年份信息和数量信息
第二步将年份信息进行格式化连续的年份取最小值和最大值并用“-”连接单独的年份单独提取出并用顿号连接
第三步写入数据到Excel中
目标实现
遍历文件新建数据存放的List
pathos.getcwd()
file_listlist(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\\,))totalNumlen(file_list[i][2])for j in range (0,len(file_list[i][2])):yearre.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]1yearList[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
wbload_workbook(表格.xlsx)
wswb.active
pathos.getcwd()
file_listlist(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\\,))totalNumlen(file_list[i][2])for j in range (0,len(file_list[i][2])):yearre.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]1yearList[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真棒