2017-12-27 863 views
2

我有一些代码在一个python脚本,看起来像创建从OpenPyXL元组:获得手机数据环行

for row in sheet.iter_rows(min_row=2, max_col=2, max_row=50) 
    print(row) 

它返回

(<Cell 'States'.A2>, <Cell 'States'.B2>) 
(<Cell 'States'.A3>, <Cell 'States'.B3>) 
... 
(<Cell 'States'.A50>, <Cell 'States'.B50>) 

我想拉值从'A-column'和'B-column'使用cell.value进一步分割'B-column'(用逗号分隔的值)(例如7,100或100,7),然后将这些值添加到字典中看起来像:

StatesPriority = { 
    A2 : (B2.ValueBeforeComma, B2.ValueAfterComma) 
    A3 : (B3.ValueBeforeComma, B3.ValueAfterComma) 
    ... 
    A50 : (B50.ValueBeforeComma, B50.ValueAfterComma) 
} 

但是,我更关心OpenPyXL函数从返回的元组中获取值。我想我可以用一点点时间用我自己的逗号分解值。

Python版本:3.6.3 OpenPyXL版本:2.4.9

所有帮助表示赞赏!

回答

1

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(',')) 
+0

,完美的工作!我不知道内置的.split。非常感谢! :) – Bill