使用python输出excel中指定行中的所有内容

时间:2014-08-27 17:55:57

标签: python excel

所以我需要输出excel数据表中指定行的所有内容,到目前为止,我可以通过输入行的数量(在while循环中)打开工作表并输出该行中的所有内容。但并非我的所有工作表都在同一行中有名称所以我需要一些东西来搜索名称(例如名称FIND)并输出其下的所有内容。有没有办法做到这一点?

 import xlrd
 file_Location ="location of file"
 workbook = xlrd.open_workbook(file_Location)
 sheet = workbook.sheet_by_name('name of the sheet im using')
 num_rows = sheet.nrows-1
 curr_row = -1

 while curr_row < num_rows:
     curr_row +=1
     row = sheet.cell(curr_row,16)
     print row.value

1 个答案:

答案 0 :(得分:0)

现在看起来您的代码正在打印同一列上不同行的值,而不是您所说的相反。如果这是您想要的,您可以使用for循环来检查第一行上每一列的值(也假设这是您想要的)。类似的东西:

for curr_col in range(50):
    col = sheet.cell(0, curr_col)

    if col.value == 'FIND':
        break

然后你会在下一个循环中使用curr_col:

while curr_row < num_rows:
    curr_row +=1
    row = sheet.cell(curr_row,curr_col)
    print row.value

如果您拥有名称的行不是电子表格中的第一行(python中的值为0),请更改

col = sheet.cell(0, curr_col)

col = sheet.cell(X, curr_col)

其中X将是您想要的行。