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:
{
'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!