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

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

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

前言

今天,在工作的時候,我的美女同事問我有沒有辦法自動生成一個這樣的表格:

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

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

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

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

原理

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

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

第三步,寫入數(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)自動整理表格的示例代碼的文章就介紹到這了,更多相關(guān)Python自動整理表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

小金县| 丰原市| 三原县| 贞丰县| 屯留县| 普宁市| 苍南县| 平陆县| 中山市| 广南县| 宁强县| 榆社县| 庆元县| 耒阳市| 琼结县| 平定县| 泰兴市| 惠水县| 若尔盖县| 湾仔区| 兖州市| 叶城县| 土默特左旗| 县级市| 洛扎县| 共和县| 甘孜| 汪清县| 余江县| 柳江县| 德昌县| 灵璧县| 襄汾县| 浮梁县| 新安县| 犍为县| 广南县| 汶上县| 三江| 古田县| 澄江县|