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

How to append specific data from excel file into a python list?

我导入了一个 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 文件中的数据附加到列表数据。如果任何列中的第一个单元格是权重,那么它会将权重行中除第一列值(权重)之外的所有数字附加到数据列表中,如下所示:

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]]

希望对您有所帮助。