使用 enumerate 和 openpyxl 遍历 Python 中的列表

Loop through a list in Python using enumerate and openpyxl

新 Python 在这里转换。简单地说,尝试使用 openpyxl 遍历 Python 中的列表。当然,我的列表大约有100个项目,我的代码有更多的条件。

lst = ['1','2','a','a12']

for value in enumerate(lst):
    row = ws1.iter_rows(min_row=value,max_row=value)

当我尝试不使用 enumerate 时出现错误 "must be str, not int" 而当我尝试使用 enumerate 时出现错误 "Can only concatenate tuple (not "int") to tuple".

我相信它与需要 intmin_rowmax_row 有关,但即便如此我还是得到“'int' 对象不可迭代”。此外,尝试使值 str()int()

非常感谢任何建议,谢谢。

当你用 enumerate 包装一个可迭代对象时,你会得到两个值:

  • 枚举索引
  • 该索引处的可迭代值

该对作为元组从 enumerate() 发出。
但是,您只标识了一个变量名,因此 value 包含整个元组,
value = (i, current_val_of_lst)

相反,尝试:

lst = ['1','2','a','a12']

for i, value in enumerate(lst):
    # i is the enumeration index
    # value is the entry in list
    ...