我导入了一个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]]
答案 0 :(得分:1)
尝试以下方法:
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]]
我希望这会有所帮助。