如何将excel文件中的特定数据附加到python列表中?

我有导入一个Excel文件。 excel文件有这样的2行5列:

Weights 1 5 9 8 Criteria Number 38 89 8 56 excel_file = tkFileDialog.askopenfilename(filetypes=[('excelfile','*.xlsx')],title='Choose a .xlsx file') n_crit = [] workbook = xlrd.open_workbook(excel_file) sheet = workbook.sheet_by_index(0) data = [] for r in range(sheet.nrows): sublist = [] for c in range(sheet.ncols): if r == "Weights": sublist.append(sheet.cell_value(r,c)) data.append(sublist) print data 

我想追加到Excel文件中的数据列表数据。 如果任何列中的第一个单元格是Weights,那么它会将除了第一列值(权重)之外的所有权重行中的数字附加到数据列表中:

 data = [[1 5 9 8]] 

尝试以下操作:

 import tkFileDialog import xlrd excel_file = tkFileDialog.askopenfilename(filetypes=[('excelfile','*.xlsx')],title='Choose a .xlsx file') workbook = xlrd.open_workbook(excel_file) sheet = workbook.sheet_by_index(0) data = [sheet.row_values(i)[1:] for i in range(sheet.nrows) if sheet.row_values(i)[0]=='Weights'] # [[1.0, 5.0, 9.0, 8.0]] 

我希望这有帮助。