使用 xlrd 遍历行号列表

Loop through a list of row numbers using xlrd

我有一个行号列表,我想使用 xlrd python 模块遍历该列表以查看这些行和第 3 列,并将这些单元格中的值保存在一个新变量中。

这是我目前的情况:

rows_exceptions_file_final = [ 2, 5, 6 , 8, 11, 15 ]

rows_string = []

for i in rows_exceptions_file_final:
    rows_string.append(sheet2.cell_value( i , 3))

我得到的异常是:

cell_value return self._cell_values[rowx][colx] IndexError: list index out of range

我为列表中的每个数字添加了 + 1,因为我之前使用过枚举函数。由于枚举从 0 开始,而我的行从 1 开始,但是在删除该 +1 函数后,问题中的代码一切正常。

的确,列和行都是从索引 0 开始的。

仅供参考:您的代码可以改进为:

# row and column index start with 0
rows_exceptions_file_final = [1, 4, 5, 7, 10, 14 ]

col_nb4 = sheet2.col_values(3, 0)
rows_string = [v for i, v in enumerate(col_nb4) if i in row_exceptions_file_final]