如何使用XLRD抓取某行某列某列的值

How to grab the value in a certain column in a certain row using XLRD

我正在尝试遍历电子表格并获取特定列下一行中单元格的值,如下所示:

# Row by row, go through the originalWorkSheet and save the values from the selected columns
numberOfRowsInOriginalWorkSheet = originalWorkSheet.nrows - 1
rowCounter = 0
while rowCounter <= numberOfRowsInOriginalWorkSheet:
    row = originalWorkSheet.row(rowCounter)
    #Grab the values in certain columns, say with the 
    # column name "Promotion" and save them to a variable

这可能吗?我的 google-foo 在这方面让我失望了。 感谢您的帮助!

有很多方法可以做到这一点,看看docs

像这样:

promotion_col_index = <promotion column index>

list_of_promotion_cells = originalWorkSheet.col(promotion_col_index)

list_of_promotion_values = [cell.value for cell in list_of_promotion_cells]

将为您提供 "Promotion" 列中值的列表

最简单的方法:

from xlrd import open_workbook


book = open_workbook(path_to_file)
sheet = book.sheet_by_index(0)
for i in range(1, sheet.nrows):
    row = sheet.row_values(i)
    variable = row[0]  # Instead zero number of certain column

或者您可以循环行列表并打印每个单元格值

book = open_workbook(path_to_file)
sheet = book.sheet_by_index(0)
for i in range(1, sheet.nrows):
    row = sheet.row_values(i)
    for cnt in range(len(row)):
       print row[cnt]

希望对您有所帮助