John Smith
John Smith

Reputation: 5

Creating a list with nested dictionaries in Openpyxl

I want to loop through all rows in my Excel sheet and store the value of each row (starting at row 2) in individual dictionaries within 1 big list.

I have a simple list of items in an Excel spanning from Column A - Column D:

Fruit:  Quantity:   Color:  Cost
Apple   5           Red     0.6
Banana  6           Yellow  0.4
Orange  4           Orange  0.3
Kiwi    2           Green   0.1

I want the very first result to look like:

[{'Fruit': 'Apple', 'Quantity': 5, 'Color': 'Red', 'Cost': 0.6}]

Here's how my code looks at the moment:

import openpyxl
wb = openpyxl.load_workbook('fruit.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')

for row in range(2, sheet.max_row + 1):
    fruit = sheet['A' + str(row)].value
    quantity = sheet['B' + str(row)].value
    color = sheet['C' + str(row)].value
    cost = sheet['D' + str(row)].value

    allFruits = [{'Fruit': fruit,
                'Quantity': quantity,
                'Color': color,
                'Cost': cost}]

print(allFruits)

When I run the code, the result prints only the very last active row in the sheet:

[{'Fruit': 'Kiwi', 'Quantity': 2, 'Color': 'Green', 'Cost': 0.1}]

I want this format for ALL the rows, not just the final row. I don't understand why the code is skipping all of the rows in between and just printing the final row. Could anyone help?

Upvotes: 0

Views: 1070

Answers (1)

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

When you assign to allFruits inside your loop you overwrite it on each iteration.

Instead define the allFruits list outside of your loop and call allFruits.append() inside the loop to add each fruit dictionary.

allFruits = []

for row in range(2, sheet.max_row + 1):
    fruit = sheet['A' + str(row)].value
    quantity = sheet['B' + str(row)].value
    color = sheet['C' + str(row)].value
    cost = sheet['D' + str(row)].value

    allFruits.append({'Fruit': fruit,
                'Quantity': quantity,
                'Color': color,
                'Cost': cost})

You could also shorten your code up by doing:

allFruits = []
key_col = [('Fruit', 'A'), ('Quantity', 'B'), ('Color', 'C'), ('Cost', 'D')]

for row in range(2, sheet.max_row + 1):
    allFruits.append({key:sheet[col+str(row)].value for (key, col) in key_col})

Upvotes: 3

Related Questions