Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generating dictionary dynamically in Python 3?

I have the following list

['BILL FROM:', 'TestCorp', 'USA, NY 02123, Washington St.', 'New York, NY, 02123', 'United States', 'musicjohnliofficial@gmail.com', 'BILL TO:', 'Yao Min', 'LA, 2123, Los Angeles', 'new York, NY, 9803432', 'United States', 'yao123@mail.com', 'INVOICE #', '01', 'INVOICE DATE', '2022-08-05', 'AMOUNT DUE', '$36.79', 'SUBTOTAL', '$36.74', 'TAX (0.13%)', '$0.05', 'TOTAL', '$36.79', 'Item', 'Description', 'Quantity', 'Unit Cost', 'Line Total', 'Pen', 'Pen, black coloe', '1.5', '$1.16', '$1.74', 'Book', 'Calculus, Zorych, pt. 1', '3.5', '$10.00', '$35.00', 'Powered by Shopify', 'www.shopify.com']

And i need to generate a dictionary from it, so that my dictionary could be like that:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    'items': [
    {
        'item': 'Pen',
        'description': 'Pen, black coloe',
        'quantity': '1.5',
        'lineCost': '$1.16',
        'lineTotaal': '$1.74'
    },
    {
        'item': 'Book',
        'description': 'Calculus, Zorych, pt. 1',
        'quantity': '3.5',
        'lineCost': '$10.00',
        'lineTotaal': '$35.00'
    }
  ]
}

I tried to loop through the list and append needed data to separate dict but it gave me duplicate values:

for i in range(len(text['blocks'])):
for j in range(len(text['blocks'][i]['lines'])):
    for k in range(len(text['blocks'][i]['lines'][j]['spans'])):
        prepared_data.append(text['blocks'][i]['lines'][j]['spans'][k]['text'])
        cleaned_json_keys = text['blocks'][i]['lines'][0]['spans'][0]['text']
        cleaned_json_vals = text['blocks'][i]['lines'][j]['spans'][k]['text']
        cleaned_json[cleaned_json_keys] = cleaned_json_vals

Output:

{'BILL FROM:': 'BILL FROM:', 'TestCorp': 'TestCorp', 'USA, NY 02123, Washington St.': 'USA, NY 02123, Washington St.', 'New York, NY, 02123': 'New York, NY, 02123', 'United States': 'United States', 'musicjohnliofficial@gmail.com': 'musicjohnliofficial@gmail.com', 'BILL TO:': 'BILL TO:', 'Yao Min': 'Yao Min', 'LA, 2123, Los Angeles': 'LA, 2123, Los Angeles', 'new York, NY, 9803432': 'new York, NY, 9803432', 'yao123@mail.com': 'yao123@mail.com', 'INVOICE #': '01', 'INVOICE DATE': '2022-08-05', 'AMOUNT DUE': '$36.79', 'SUBTOTAL': '$36.74', 'TAX (0.13%)': '$0.05', 'TOTAL': '$36.79', 'Item': 'Line Total', 'Pen': '$1.74', 'Book': '$35.00', 'Powered by Shopify': 'Powered by Shopify', 'www.shopify.com': 'www.shopify.com'}

How could I do that? I somebody knows the way, help me please… Thank you in advance!

>Solution :

I will help you a bit: you need to parse that list to extract the relevant data. The list could vary in length, but provided that the items are always located between ‘Line Total’ and ‘Powered by Shopify’ then you can perform slicing after locating the data boundaries:

>>> my_list.index("Line Total")
28
>>> my_list.index("Powered by Shopify")
39
items = my_list[29:39]

>>> items
['Pen', 'Pen, black coloe', '1.5', '$1.16', '$1.74', 'Book', 'Calculus, Zorych, pt. 1', '3.5', '$10.00', '$35.00']

The length of the resulting list should be a multiple of five in your case. Then, before you start looping on that I suggest you split that list into sublists of 5 items each. Have a look here to see how it could be done. This function borrowed from the above quoted post will do the job just fine:

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

Result (applying list because what is returned is a generator):

>>> list(chunks(items, 5))
[['Pen', 'Pen, black coloe', '1.5', '$1.16', '$1.74'], ['Book', 'Calculus, Zorych, pt. 1', '3.5', '$10.00', '$35.00']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading