使用python3+xlrd解析Excel的实例

作者:lm_y 时间:2021-05-09 23:37:33 

实例如下所示:


# -*- coding: utf-8 -*-
import xlrd
def open_excel(file = 'file.xls'):#打开要解析的Excel文件
 try:
   data = xlrd.open_workbook(file)
   return data
 except Exception as e:
   print(e)

def excel_by_index(file = 'file.xls', colindex = 0, by_index = 0):#按表的索引读取
 data = open_excel(file)#打开excel文件
 tab = data.sheets()[by_index]#选择excel里面的Sheet
 nrows = tab.nrows#行数
 ncols = tab.ncols#列数
 colName = tab.row_values(colindex)#第0行的值
 list = []#创建一个空列表
 for x in range(0, nrows):
   row = tab.row_values(x)
   if row:
     app = {}#创建空字典
     for y in range(0, ncols):
       app [ colName[y] ] = row[y]
     list.append(app)
 return list

def read_excel(file = 'file.xls', by_index = 0):#直接读取excel表中的各个值
 data = open_excel(file)#打开excel文件
 tab = data.sheets()[by_index]#选择excel里面的Sheet
 nrows = tab.nrows#行数
 ncols = tab.ncols#列数
 for x in range(0, nrows):
    for y in range(0, ncols):
      value = tab.cell(x,y).value
      print(tab.cell(x, y).value)
def main():
 # print('input the path of your file:')
 # a = open_excel(r'D:\smt_ioe\untitled\analysis_excel\my.xls')
 # print(a)
 b = excel_by_index(r'D:\smt_ioe\untitled\analysis_excel\my.xls', 0, 2)
 m = []
 for i in range(b.__len__()):
   c = b[i]
   # a = c['name']
 for x in c:
   if x == 'date':
     print(x)
 print('meng')
 read_excel(r'D:\smt_ioe\untitled\analysis_excel\my.xls',2)

if __name__ == '__main__':
 main()

来源:https://blog.csdn.net/Com_ma/article/details/76735389

标签:python3,xlrd,Excel
0
投稿

猜你喜欢

  • Oracle数据仓库的分层管理器解决方案开发者网络Oracle

    2010-07-16 13:08:00
  • 请问[\\u4E00-\\u9FA5]是什么正则表达式码

    2009-07-10 13:10:00
  • python opencv 检测移动物体并截图保存实例

    2023-11-05 16:10:35
  • 通过源码分析Golang cron的实现原理

    2023-06-15 23:49:24
  • oracle命令行删除与创建用户

    2008-01-02 17:01:00
  • 为SWFUpload增加ASP版本的上传处理程序

    2011-04-28 08:01:00
  • js中如何对url进行编码和解码

    2023-07-22 03:45:08
  • 两个不太常用的 CSS Hack

    2008-06-27 12:49:00
  • php实现的验证码文件类实例

    2023-08-17 17:54:52
  • 合理设置内存让数据库与其他程序共存

    2009-05-21 16:24:00
  • asp中格式化HTML函数代码 SDCMS加强版

    2011-02-20 11:18:00
  • 面向对象CSS FAQ[译]

    2009-10-27 15:59:00
  • 用来武装Firefox的24款Web开发插件

    2010-02-28 12:40:00
  • PHP使用星号隐藏用户名,手机和邮箱的实现方法

    2023-11-05 10:41:56
  • 模拟下拉菜单[兼容IE系列以及火狐]

    2009-12-13 10:23:00
  • 怎样取得局域网中所有SQL Server的实例

    2009-01-08 13:20:00
  • 用python制作游戏外 挂

    2023-08-03 15:55:43
  • ASP+ajax注册即时提示程序代码

    2011-02-05 11:25:00
  • asp如何获知Connection对象更多的信息?

    2009-11-23 20:53:00
  • 微信小程序request请求后台接口php的实例详解

    2023-11-11 14:24:04
  • asp之家 网络编程 m.aspxhome.com