Python實現(xiàn)自動整理表格的示例代碼
前言
今天,在工作的時候,我的美女同事問我有沒有辦法自動生成一個這樣的表格:
第一列是院校+科目,第二列是年份,第三列是數(shù)量。
這張表格是基于這一文件夾填充的,之前要一個文件夾一個文件夾打開然后手動填寫年份和數(shù)量
手動整理需要耗費較長時間,于是我便開發(fā)了一個 Python 程序用來自動生成歸納表格
利用正則表達(dá)式+OS庫+openpyxl生成真題年份歸納表格
原理
第一步,遍歷文件夾下的所有文件和子文件夾的名稱,并獲取子文件夾下的文件的年份信息和數(shù)量信息
第二步,將年份信息進(jìn)行格式化,連續(xù)的年份取最小值和最大值,并用“-”連接,單獨的年份單獨提取出,并用頓號連接
第三步,寫入數(shù)據(jù)到Excel中
目標(biāo)實現(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=[]
運行效果
好啦,程序不復(fù)雜,不過卻大大提高了工作效率,不得不說,Python真棒!
到此這篇關(guān)于Python實現(xiàn)自動整理表格的示例代碼的文章就介紹到這了,更多相關(guān)Python自動整理表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
將tf.batch_matmul替換成tf.matmul的實現(xiàn)
這篇文章主要介紹了將tf.batch_matmul替換成tf.matmul的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06使用tensorflow實現(xiàn)VGG網(wǎng)絡(luò),訓(xùn)練mnist數(shù)據(jù)集方式
這篇文章主要介紹了使用tensorflow實現(xiàn)VGG網(wǎng)絡(luò),訓(xùn)練mnist數(shù)據(jù)集方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python內(nèi)置數(shù)據(jù)結(jié)構(gòu)列表與元組示例詳解
這篇文章主要給大家介紹了關(guān)于Python內(nèi)置數(shù)據(jù)結(jié)構(gòu)列表與元組的相關(guān)資料,列表是順序存儲的數(shù)據(jù)結(jié)構(gòu),類似于數(shù)據(jù)結(jié)構(gòu)中的順序表,在存儲上是相連的一大塊內(nèi)存空間,在物理和邏輯上都是連續(xù)的,需要的朋友可以參考下2021-08-08