python遍历文件夹下所有excel文件

yipeiwu_com5年前Python基础

大数据处理经常要用到一堆表格,然后需要把数据导入一个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") 

欢迎各种不一样的想法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现H2O中的随机森林算法介绍及其项目实战

python实现H2O中的随机森林算法介绍及其项目实战

H2O中的随机森林算法介绍及其项目实战(python实现) 包的引入:from h2o.estimators.random_forest import H2ORandomForestEs...

web.py在SAE中的Session问题解决方法(使用mysql存储)

这段时间一直想尝试着在SAE中使用Python,初步选择了Web.py框架做为开发框架,但是可怜SAE上的资料少的可怜,有点问题基本上解决不了,今天解决一个Session在Session...

python 实现登录网页的操作方法

有些网页需要你登录之后才可以访问,你需要提供账户和密码。 只要在发送http请求时,带上含有正常登陆的cookie就可以了。 1、首先我们要先了解cookie的工作原理。 Cookie是...

Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】

本文实例讲述了Python tensorflow实现mnist手写数字识别。分享给大家供大家参考,具体如下: 非卷积实现 import tensorflow as tf from t...

python 扩展print打印文件路径和当前时间信息的实例代码

pinrt函数我们经常使用,但是有时候python自带的print函数打印的信息不够详细,我们可以扩展一下,打印更多的信息,例如程序文件绝对路径、当前日期时间、消息等等。这里我参考了yd...