最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)自動(dòng)整理表格的示例代碼

 更新時(shí)間:2023年03月02日 11:21:16   作者:了不起  
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)自動(dòng)整理表格的功能,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

前言

今天,在工作的時(shí)候,我的美女同事問我有沒有辦法自動(dòng)生成一個(gè)這樣的表格:

第一列是院校+科目,第二列是年份,第三列是數(shù)量。

這張表格是基于這一文件夾填充的,之前要一個(gè)文件夾一個(gè)文件夾打開然后手動(dòng)填寫年份和數(shù)量

手動(dòng)整理需要耗費(fèi)較長時(shí)間,于是我便開發(fā)了一個(gè) Python 程序用來自動(dòng)生成歸納表格

利用正則表達(dá)式+OS庫+openpyxl生成真題年份歸納表格

原理

第一步,遍歷文件夾下的所有文件和子文件夾的名稱,并獲取子文件夾下的文件的年份信息和數(shù)量信息

第二步,將年份信息進(jìn)行格式化,連續(xù)的年份取最小值和最大值,并用“-”連接,單獨(dú)的年份單獨(dú)提取出,并用頓號(hào)連接

第三步,寫入數(shù)據(jù)到Excel中

目標(biāo)實(shí)現(xiàn)

遍歷文件,新建數(shù)據(jù)存放的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)

保存數(shù)據(jù)并輸出到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=[]

運(yùn)行效果

好啦,程序不復(fù)雜,不過卻大大提高了工作效率,不得不說,Python真棒!

到此這篇關(guān)于Python實(shí)現(xiàn)自動(dòng)整理表格的示例代碼的文章就介紹到這了,更多相關(guān)Python自動(dòng)整理表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

博爱县| 东海县| 会昌县| 新竹市| 玉山县| 合作市| 济宁市| 枣阳市| 简阳市| 尼木县| 惠州市| 铁力市| 新营市| 庆城县| 商城县| 林州市| 永修县| 濉溪县| 隆尧县| 洛浦县| 大邑县| 独山县| 于田县| 沙田区| 吕梁市| 巨野县| 德保县| 璧山县| 岑巩县| 墨脱县| 东丽区| 石家庄市| 瓮安县| 简阳市| 太仆寺旗| 新闻| 静宁县| 聂荣县| 巴彦淖尔市| 勐海县| 四会市|