简体   繁体   English

Python,将Excel文件加载到字典

[英]Python, load Excel file to Dictionary

I'm losing my mind here.我在这里失去理智。 I don't get why it saves only the last row to the dictionary.我不明白为什么它只将最后一行保存到字典中。 I want to save the whole excel table to Dictionary so I can group those values by Key(columnName) and then do something with those values so I can access each cell based on the row number.我想将整个 excel 表保存到字典中,以便我可以按 Key(columnName) 对这些值进行分组,然后对这些值执行某些操作,以便我可以根据行号访问每个单元格。

import openpyxl
from pathlib import Path

data = {}
data['dict1'] = {}

xlsx_file = Path(Path.home(), 'FormaterPythonUniversity', 'Template.xlsx')


wb_obj = openpyxl.load_workbook(xlsx_file) 

# Read the active sheet:
sheet = wb_obj.active


for i, row in enumerate(sheet.iter_rows(values_only=True)):
    data['Full Name'] = row[0]
    print(row[0])
    data['dict1']['Work email address'] = row[1]
    data['dict1']['Start date'] = row[2]
    data['dict1']['Manager name'] = row[3]
    data['dict1']['Manager email address'] = row[4]
    data['dict1']['Category'] = row[5]
     
print(data)      

What happens now is that only the last row of the excel file is being stored in this dictionary.现在发生的情况是该字典中仅存储了 excel 文件的最后一行。

Example Output:示例输出:

{'dict1': {'Work email address': 'smth.@gmail.com', 'Start date': '2021-11-11', 'Manager name': 'Paul Smth', 'Manager email address': 'PaulSmth@gmail.com', 'Category': 'Owner'}, 'Full Name': 'Smth Smth'}

Excel spreadsheet I'm using:我正在使用的 Excel 电子表格:

Screen grab of the Excel sheet Excel 工作表的屏幕截图

I do not understand why it doesn't load the whole thing into the dictionary, and just the info from the last row.我不明白为什么它不将整个内容加载到字典中,而只加载最后一行的信息。

You are saving each row in the same keys of the dictionary data['dict1'].您将每一行保存在字典 data['dict1'] 的相同键中。 Make data a list, and append a series of new dictionaries to it instead:使data成为一个列表,并在其中附加一系列新词典:

data = []
for i, row in enumerate(sheet.iter_rows(values_only=True)):
    rowdict = dict()
    rowdict['Work email address'] = row[1]
    rowdict['Start date'] = row[2]
    # (etc.)
    data.append(rowdict)

Done.完毕。

If I understood your problem correctly, it is rooted in the fact that you are overwriting the value of each key in every iteration of the loop (ie corresponding to each spreadsheet row).如果我正确理解了您的问题,则根源在于您在循环的每次迭代中都覆盖了每个键的值(即对应于每个电子表格行)。 Try this for each column before the FOR loop:在 FOR 循环之前为每一列尝试这个:

data['dict1']['Work email address'] = []

and then append each newly read row of the sheet to the lists stored in your dictionary.然后将每个新阅读的工作表行附加到存储在字典中的列表中。 So you'll have something like below in your loop:所以你的循环中会有类似下面的内容:

data['dict1']['Work email address'].append(row[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM