2017-06-15 47 views

回答

0

使用xlrd,您可以将一个excel数据表,词典列表:

import xlrd 

workbook = xlrd.open_workbook('foo.xls') 
workbook = xlrd.open_workbook('foo.xls', on_demand = True) 
worksheet = workbook.sheet_by_index(0) 
first_row = [] # The row where we stock the name of the column 
for col in range(worksheet.ncols): 
    first_row.append(worksheet.cell_value(0,col)) 
# transform the workbook to a list of dictionaries 
data =[] 
for row in range(1, worksheet.nrows): 
    elm = {} 
    for col in range(worksheet.ncols): 
     elm[first_row[col]]=worksheet.cell_value(row,col) 
    data.append(elm) 
print data 

您还可以使用Pandas

from pandas import * 
xls = ExcelFile('flow charts.xls') 
df = xls.parse(xls.sheet_names[0]) 
print df.to_dict() 
相关问题