本文共 1271 字,大约阅读时间需要 4 分钟。
import xdrlib ,sys import xlrd def open_excel(file= 'file.xls'): try : data = xlrd.open_workbook(file) return data except Exception,e: print str(e) def excel_table_byindex(file= 'file.xls',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 def excel_table_byname(file= 'file.xls',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() 转载地址:http://bwcvl.baihongyu.com/