使用 openpyxl 时,如何根据我在 Excel sheet 中迭代的行号创建动态变量?
How do I create a dynamic variable based on the row number I am iterating over in an Excel sheet while using openpyxl?
我正在尝试遍历 Excel 工作表中的所有行,并根据当前正在遍历的单元格设置一个变量。下面是我正在使用的代码:
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook('workbook.xlsx', data_only=True)
queue = wb['Queue']
max_row = queue.max_row
for i in queue.iter_rows(min_row=2, max_row=max_row):
dynamic_cell = queue.cell(row=i, column=10).value
if dynamic_cell == 'ice cream':
# perform some operation
最初,我认为这篇文章在“按行迭代”部分下提供了我正在寻找的答案:https://medium.com/aubergine-solutions/working-with-excel-sheets-in-python-using-openpyxl-4f9fd32de87f
然而,当我到达输入时:
dynamic_cell = queue.cell(row=i, column=10).value
我的输出是:
TypeError: '<' not supported between instances of 'tuple' and 'int'
我不确定如何解释这个问题或尝试其他解决方案。非常感谢任何关于如何使 dynamic_cell
工作的反馈! (:
更新: 根据我收集到的信息,最有效的解决方案可能是根据 [=16= 中的行数创建一个整数列表](参见:How can you dynamically create variables via a while loop?)。但是,我目前没有必要的经验来完成这项工作。我将继续寻找解决方案,同时定期检查此 post。
根据@Charlie Clark 的观察,解决方案:
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook('workbook.xlsx', data_only=True)
queue = wb['Queue']
max_row = queue.max_row
for i in queue.iter_rows(min_row=2, max_row=max_row):
dynamic_cell = i[9]
if dynamic_cell == 'ice cream':
# perform some operation
我正在尝试遍历 Excel 工作表中的所有行,并根据当前正在遍历的单元格设置一个变量。下面是我正在使用的代码:
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook('workbook.xlsx', data_only=True)
queue = wb['Queue']
max_row = queue.max_row
for i in queue.iter_rows(min_row=2, max_row=max_row):
dynamic_cell = queue.cell(row=i, column=10).value
if dynamic_cell == 'ice cream':
# perform some operation
最初,我认为这篇文章在“按行迭代”部分下提供了我正在寻找的答案:https://medium.com/aubergine-solutions/working-with-excel-sheets-in-python-using-openpyxl-4f9fd32de87f
然而,当我到达输入时:
dynamic_cell = queue.cell(row=i, column=10).value
我的输出是:
TypeError: '<' not supported between instances of 'tuple' and 'int'
我不确定如何解释这个问题或尝试其他解决方案。非常感谢任何关于如何使 dynamic_cell
工作的反馈! (:
更新: 根据我收集到的信息,最有效的解决方案可能是根据 [=16= 中的行数创建一个整数列表](参见:How can you dynamically create variables via a while loop?)。但是,我目前没有必要的经验来完成这项工作。我将继续寻找解决方案,同时定期检查此 post。
根据@Charlie Clark 的观察,解决方案:
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook('workbook.xlsx', data_only=True)
queue = wb['Queue']
max_row = queue.max_row
for i in queue.iter_rows(min_row=2, max_row=max_row):
dynamic_cell = i[9]
if dynamic_cell == 'ice cream':
# perform some operation