python遍歷文件夾下所有excel文件
大數(shù)據(jù)處理經(jīng)常要用到一堆表格,然后需要把數(shù)據(jù)導(dǎo)入一個list中進行各種算法分析,簡單講一下自己的做法:
1.如何讀取excel文件
網(wǎng)上的版本很多,在xlrd模塊基礎(chǔ)上,找到一些源碼:
import xdrlib ,sys import xlrd def open_excel(file="C:/Users/flyminer/Desktop/新建 Microsoft Excel 工作表.xlsx"): data = xlrd.open_workbook(file) return data #根據(jù)索引獲取Excel表格中的數(shù)據(jù) 參數(shù):file:Excel文件路徑 colnameindex:表頭列名所在行的所以 ,by_index:表的索引 def excel_table_byindex(file="C:/Users/flyminer/Desktop/新建 Microsoft Excel 工作表.xlsx",colnameindex=0,by_index=0): data = open_excel(file) table = data.sheets()[by_index] nrows = table.nrows #行數(shù) ncols = table.ncols #列數(shù) colnames = table.row_values(colnameindex) #某一行數(shù)據(jù) list =[] for rownum in range(1,nrows): row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return list #根據(jù)名稱獲取Excel表格中的數(shù)據(jù) 參數(shù):file:Excel文件路徑 colnameindex:表頭列名所在行的所以 ,by_name:Sheet1名稱 def excel_table_byname(file="C:/Users/flyminer/Desktop/新建 Microsoft Excel 工作表.xlsx",colnameindex=0,by_name=u'Sheet1'): data = open_excel(file) table = data.sheet_by_name(by_name) nrows = table.nrows #行數(shù) colnames = table.row_values(colnameindex) #某一行數(shù)據(jù) list =[] for rownum in range(1,nrows): row = table.row_values(rownum) if row: app = {} for i in range(len(colnames)): app[colnames[i]] = row[i] list.append(app) return list def main(): tables = excel_table_byindex() for row in tables: print(row) tables = excel_table_byname() for row in tables: print(row) if __name__=="__main__": main()
最后一句是重點,所以這里也給代碼人點個贊!
最后一句讓代碼里的函數(shù)都可以被復(fù)用,簡單地說:假設(shè)文件名是a,在程序中import a以后,就可以用a.excel_table_byname()和a.excel_table_byindex()這兩個超級好用的函數(shù)了。
2.然后是遍歷文件夾取得excel文件以及路徑:,原創(chuàng)代碼如下:
import os import xlrd import test_wy xpath="E:/唐偉捷/電力/電力系統(tǒng)總文件夾/舟山電力" xtype="xlsx" typedata = [] name = [] raw_data=[] file_path=[] def collect_xls(list_collect,type1): #取得列表中所有的type文件 for each_element in list_collect: if isinstance(each_element,list): collect_xls(each_element,type1) elif each_element.endswith(type1): typedata.insert(0,each_element) return typedata #讀取所有文件夾中的xls文件 def read_xls(path,type2): #遍歷路徑文件夾 for file in os.walk(path): for each_list in file[2]: file_path=file[0]+"/"+each_list #os.walk()函數(shù)返回三個參數(shù):路徑,子文件夾,路徑下的文件,利用字符串拼接file[0]和file[2]得到文件的路徑 name.insert(0,file_path) all_xls = collect_xls(name, type2) #遍歷所有type文件路徑并讀取數(shù)據(jù) for evey_name in all_xls: xls_data = xlrd.open_workbook(evey_name) for each_sheet in xls_data.sheets(): sheet_data=test_wy.excel_table_byname(evey_name,0,each_sheet.name) #請參考讀取excel文件的代碼 raw_data.insert(0, sheet_data) print(each_sheet.name,":Data has been done.") return raw_data a=read_xls(xpath,xtype) print("Victory")
歡迎各種不一樣的想法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
django 刪除數(shù)據(jù)庫表后重新同步的方法
今天小編就為大家分享一篇django 刪除數(shù)據(jù)庫表后重新同步的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05解決selenium+Headless Chrome實現(xiàn)不彈出瀏覽器自動化登錄的問題
這篇文章主要介紹了解決selenium+Headless Chrome實現(xiàn)不彈出瀏覽器自動化登錄的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01一文掌握6種Python中常用數(shù)據(jù)庫操作及代碼
在數(shù)據(jù)處理和管理領(lǐng)域,Python作為一種高效、易用的編程語言,擁有豐富的數(shù)據(jù)庫操作模塊,可以輕松實現(xiàn)對關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)操作,本文將介紹六種常見的Python數(shù)據(jù)庫操作模塊,需要的可以參考下2023-12-12python利用joblib進行并行數(shù)據(jù)處理的代碼示例
在數(shù)據(jù)量比較大的情況下,數(shù)據(jù)預(yù)處理有時候會非常耗費時間,可以利用 joblib 中的 Parallel 和 delayed 進行多CPU并行處理,文中給出了詳細的代碼示例,需要的朋友可以參考下2023-10-10python3+PyQt5 自定義窗口部件--使用窗口部件樣式表的方法
今天小編就為大家分享一篇python3+PyQt5 自定義窗口部件--使用窗口部件樣式表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python中scipy.stats產(chǎn)生隨機數(shù)實例講解
在本篇文章里小編給大家分享的是一篇關(guān)于python中scipy.stats產(chǎn)生隨機數(shù)實例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-02-02