我有一个Excel文件,看起来像
。
对于每一个名字,我想为每一组添加各自的单元格,所以我希望一个for循环迭代+4行来遍历所有的名字。
以下是我目前所做的工作:
import openpyxl
doc = openpyxl.load_workbook('World Cup Bet Tournament.xlsx')
doc_activation = doc.active
############################################
""" Creating the final dictionary """
final_dict = {}
groups_dict = {}
group_list = []
############################################
for row_1 in range(2, 42):
for col_1 in doc_activation.iter_cols(1, 1):
name = col_1[row_1].value
if name is None:
break
else:
final_dict[name] = groups_dict
for row_2 in range(1, 2):
for col_2 in doc_activation.iter_cols(2, 9):
group = col_2[row_2].value
groups_dict[group] = group_list
print(final_dict)
和输出:
{'1. Mathias L.R': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '2. Noah L.R': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '3. Jessy P.N': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '4. Enzo B.': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '5. Savio M.': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '6. Jonathan M.': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '7. Hans M.': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '8. J-E': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '9. Schadrac ': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}, '10. Mathieu G.': {'GROUP A': [], 'GROUP B': [], 'GROUP C': [], 'GROUP D': [], 'GROUP E': [], 'GROUP F': [], 'GROUP G': [], 'GROUP H': []}}
因此,对于每个代表名字的键,都有一个字典值,这个字典的键是每个组的名字。它的值是一个列表,包含每个国家对应的球员。
1条答案
按热度按时间jyztefdp1#
您只需要根据单元格中的值构建用户组,然后将其添加到以组名为关键字的组字典中,例如GROUPA、GROUPB等,然后将其添加到用户名下的整个字典中。
请参阅示例代码
输出量