[英]Getting cell data from openpyxl looped rows
I have some code to create tuples from OpenPyXL in a python script that looks like:我有一些代码可以在 python 脚本中从 OpenPyXL 创建元组,如下所示:
for row in sheet.iter_rows(min_row=2, max_col=2, max_row=50)
print(row)
Which returns哪个返回
(<Cell 'States'.A2>, <Cell 'States'.B2>)
(<Cell 'States'.A3>, <Cell 'States'.B3>)
...
(<Cell 'States'.A50>, <Cell 'States'.B50>)
I would like to pull the values from the 'A-column' and the 'B-column' by using cell.value and further splitting the 'B-column' by a comma delimited value (ex. 7,100 or 100,7) then adding the values to a dictionary that would look like:我想通过使用 cell.value 并通过逗号分隔值(例如 7,100 或 100,7)进一步拆分“B 列”从“A 列”和“B 列”中提取值将值添加到如下所示的字典中:
StatesPriority = {
A2 : (B2.ValueBeforeComma, B2.ValueAfterComma)
A3 : (B3.ValueBeforeComma, B3.ValueAfterComma)
...
A50 : (B50.ValueBeforeComma, B50.ValueAfterComma)
}
However, I am more concerned about the OpenPyXL function of getting values from a returned tuple.但是,我更关心从返回的元组中获取值的 OpenPyXL function。 I think I can figure out splitting values by commas on my own with a little bit of time.我想我可以花一点时间自己找出用逗号分隔的值。
Python Version: 3.6.3 OpenPyXL Version: 2.4.9 Python 版本:3.6.3 OpenPyXL 版本:2.4.9
All help is appreciated!感谢所有帮助!
row
is a tuple with two elements, therefore you can unpack it during assignment: row
是一个包含两个元素的元组,因此您可以在赋值期间解压缩它:
StatesPriority = {}
for row in sheet.iter_rows(min_row=2, max_col=2, max_row=50)
cell_a, cell_b = row
StatesPriority[cell_a.value] = (tuple(cell_b.value.split(','))
After some research, I believe the new method is to use values_only=True like below:经过一番研究,我相信新方法是使用values_only=True ,如下所示:
for row in sheet.iter_rows(min_row=2, max_col=2, max_row=50, values_only=True)
print(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.