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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用正則表達式報錯:nothing?to?repeat?at?position?0的解決方案
今天在使用python 正則模塊匹配字符串時遇到了這個問題,分享給大家,這篇文章主要給大家介紹了關(guān)于Python使用正則表達式報錯nothing?to?repeat?at?position?0的解決方案,需要的朋友可以參考下2023-03-03
pandas基礎(chǔ)?Series與Dataframe與numpy對二進制文件輸入輸出
這篇文章主要介紹了pandas基礎(chǔ)Series與Dataframe與numpy對二進制文件輸入輸出,series是一種一維的數(shù)組型對象,它包含了一個值序列和一個數(shù)據(jù)標簽2022-07-07

