python遍历文件夹下所有excel文件
作者:法克大米 时间:2023-10-26 17:57:12
大数据处理经常要用到一堆表格,然后需要把数据导入一个list中进行各种算法分析,简单讲一下自己的做法:
1.如何读取excel文件
网上的版本很多,在xlrd模块基础上,找到一些源码:
import xdrlib ,sys
import xlrd
def open_excel(file="C:/Users/flyminer/Desktop/新建 Microsoft Excel 工作表.xlsx"):
data = xlrd.open_workbook(file)
return data
#根据索引获取Excel表格中的数据 参数: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 #行数
ncols = table.ncols #列数
colnames = table.row_values(colnameindex) #某一行数据
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
#根据名称获取Excel表格中的数据 参数: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 #行数
colnames = table.row_values(colnameindex) #某一行数据
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()
最后一句是重点,所以这里也给代码人点个赞!
最后一句让代码里的函数都可以被复用,简单地说:假设文件名是a,在程序中import a以后,就可以用a.excel_table_byname()和a.excel_table_byindex()这两个超级好用的函数了。
2.然后是遍历文件夹取得excel文件以及路径:,原创代码如下:
import os
import xlrd
import test_wy
xpath="E:/唐伟捷/电力/电力系统总文件夹/舟山电力"
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()函数返回三个参数:路径,子文件夹,路径下的文件,利用字符串拼接file[0]和file[2]得到文件的路径
name.insert(0,file_path)
all_xls = collect_xls(name, type2)
#遍历所有type文件路径并读取数据
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")
欢迎各种不一样的想法。
来源:http://blog.csdn.net/u012013017/article/details/58082064
标签:python,遍历,excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python如何实现强制数据类型转换
2022-10-18 10:08:02
python 常见的反爬虫策略
2022-12-17 21:51:54
![](https://img.aspxhome.com/file/2023/5/107075_0s.jpg)
selenium3.0+python之环境搭建的方法步骤
2023-03-20 06:00:22
![](https://img.aspxhome.com/file/2023/7/96977_0s.png)
MYSQL数据库教程:唯一编号
2009-02-27 15:27:00
在Python的Flask框架下使用sqlalchemy库的简单教程
2021-02-23 23:58:40
mysql的group_concat函数使用示例
2024-01-22 00:10:02
Python虚拟环境venv用法详解
2023-10-26 12:37:12
![](https://img.aspxhome.com/file/2023/7/121057_0s.png)
python实现跳表SkipList的示例代码
2022-04-05 05:01:53
![](https://img.aspxhome.com/file/2023/3/114783_0s.jpg)
Golang关键字defer的用法详解
2024-02-04 13:52:45
python 循环遍历字典元素的简单方法
2023-12-15 08:06:15
python信号量,条件变量和事件详解
2021-10-06 18:14:23
![](https://img.aspxhome.com/file/2023/4/109884_0s.png)
如何使用Python+ChatGPT批量生成论文
2023-10-01 20:30:17
![](https://img.aspxhome.com/file/2023/9/63199_0s.png)
BootStrap给table表格的每一行添加一个按钮事件
2024-05-11 09:07:33
python学习笔记--将python源文件打包成exe文件(pyinstaller)
2021-04-17 22:03:22
css学习笔记:安全字体
2009-03-10 18:34:00
go中的unsafe包及使用详解
2023-10-13 17:07:27
Go外部依赖包从vendor,$GOPATH和$GOPATH/pkg/mod查找顺序
2024-04-28 10:49:59
![](https://img.aspxhome.com/file/2023/0/134120_0s.jpg)
golang beego框架路由ORM增删改查完整案例
2024-05-09 15:00:59
![](https://img.aspxhome.com/file/2023/4/127054_0s.png)
pytorch中retain_graph==True的作用说明
2021-08-03 09:15:26
vue中echarts的用法及与elementui-select的协同绑定操作
2024-05-10 14:20:13
![](https://img.aspxhome.com/file/2023/4/126404_0s.jpg)