如何使用XLRD获取某行中某列中的值

2024-06-17 12:12:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在电子表格中循环并获取某一列下某行中单元格的值,如下所示:

# 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

这可能吗?我的googlefoo在这一点上让我失败了。 谢谢你的帮助!


Tags: columnsandthefromgobysaverow
2条回答

最简单的方法是:

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

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

^{pr2}$

希望这有帮助

有很多方法可以做到这一点,看看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]

将为您提供“促销”列中的值列表

相关问题 更多 >