赞
踩
工作中经常需要处理Excel,所以自己稍微写了一个函数,方便以后自己使用.大家也可以借鉴下.
返回一个列表,里面是根据首行Name形成的字典.应该能处理大多数excel.
- # -*- coding: utf-8 -*-
- import xlrd
-
-
- def read_excel(excel_file_name='hello.xlsx', sheet_name='Sheet1', start_row=0, end_row=0, start_col=0, end_col=0 ):
- try:
- data = xlrd.open_workbook(excel_file_name)
- except Exception as e:
- print(str(e))
- table = data.sheet_by_name(sheet_name)
- if end_row == 0:
- end_row = table.nrows
- column_names = table.row_values(start_row)
- if end_col == 0:
- end_col = len(column_names)
- excel_list = []
- for rownum in range(start_row+1, end_row):
- row = table.row_values(rownum)
- if row:
- app = {}
- for i in range(start_col, end_col):
- app[column_names[i]] = row[i]
- excel_list.append(app)
- return excel_list
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。