How to create nested dictionaries from excel sheet using Openpyxl

给你一囗甜甜゛ 提交于 2020-03-05 06:06:24

问题


I am new to programming and am trying to do the following (so far unsuccessfully):

I have an excel spreadsheet with product codes, sellers and prices and am trying to update the prices in my spreadsheet from two different spreadsheets sent by wholesalers.

I want a program to correctly

  • search an excel spreadsheet and
  • copy the corresponding price from there to the correct location above.

So I want to, for example:

  • download the price for product code '92526' sold by wholesaler A.

I want to leave the manufacturer prices untouched.

I had a program which was doing that correctly,
except that I was telling it to iterate for every line in my spreadsheet and it was erasing the existing prices for manufacturers.

I want a dictionary in the format:

{92526: {'price': 5.5, 'seller': 'Wholesaler A'}, 
 97056: {'price': 19, 'seller': 'Wholesaler A'}, 
 ...
} 

I tried adding a screenshot of a spreadsheet with sample data but couldn't, so here it goes:

Product Code    Seller         Price
92526         Wholesaler A    5.5
97056         Wholesaler A    19
97055         Wholesaler B    15
97054         Wholesaler B    4.5
925AAT        Manufacturer    3.99
925AAF        Manufacturer    6.75

Columns are not representative of the actual columns in my spreadsheet.

The code I have is this (again, beginner):

import openpyxl
import pprint

data = {}
files = {'My_main_file':'my_file.xlsx',
         'File_WholesalerA':'FileA.xlsx',
         'File_WholesalerB':'FileB.xlsx'
         }

wb1 = openpyxl.load_workbook(files['My_main_file'])                   
wb2 = openpyxl.load_workbook(files['File_WholesalerA'])                
wb3 = openpyxl.load_workbook(files['File_WholesalerB'])           
sheet1 = wb1.get_sheet_by_name('Master Database')
sheet2 = wb2.get_sheet_by_name('sheetA')
sheet3 = wb3.get_sheet_by_name('sheetB')

# Collect all product codes in my database spreadsheet and add them as keys to the empty dictionary
for row in range(2, sheet1.max_row + 1):
    code = sheet1['E' + str(row)].value
    data[code] = code

# Get Wholesaler A prices and add them to data dictionary
for row in range(2, sheet2.max_row + 1):
    code = sheet2['A' + str(row)].value
    if code in data:
        data[code]['price'] = sheet2['J' + str(row)].value
        data[code]['seller'] = 'Wholesaler A'

# Get Wholesaler B prices and add them to prices dictionary
for row in range(2, sheet3.max_row + 1):
    code = sheet3['A' + str(row)].value
    if code in data:
        data[code]['price'] = sheet3['K' + str(row)].value
        data[code]['seller'] = 'Wholesaler B'

# Paste the prices collected into the dictionary into my excel sheet for each #corresponding product code
for row in range(2, sheet1.max_row + 1):
    code = sheet1['E' + str(row)].value
    if code in data:
        # Here I try to ensure that the code only updates the prices for the 
        # corresponding sellers and doesn't overwrite the prices for 
        # manufacturers.
        if sheet1['C' + str(row)].value == data[code]['seller']:
            sheet1['K' + str(row)].value = data[code]['price']

# Save another version of the spreadsheet with the data
wb1.save('My_main_file v2.xlsx')

pprint.pprint(data)

The expected result is for the program to scan the (10k +) lines of the Wholesaler spreadsheets, find the price corresponding to my product code and paste it into my sheet overwriting the old price but not erasing any other price.

My initial program managed to collect the prices and paste them to the corresponding product codes, but it erased the manufacturer prices which I had. This new program wont even populate the dictionary for some reason.

Any help is appreciated.


回答1:


This assignment can't be done

data[code]['price'] = sheet2['J' + str(row)].value
data[code]['seller'] = 'Wholesaler A'

You can try this structure if it helps you out

data[code] = {'price': sheet2['J' + str(row)].value,
              'seller': 'Wholesaler A'}

Edit:

The assignment I mentioned above can be done, but your dictionary needs to know it has nested dictionary inside it as Tomerikoo commented above me, you could initial the line data[code] = {} and then it will work too. Right now you'll jsut get an error saying: TypeError: 'str'/'int'/etc object does not support item assignment



来源:https://stackoverflow.com/questions/58065135/how-to-create-nested-dictionaries-from-excel-sheet-using-openpyxl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!