I have a file as content a list of dictionaries (Around 75000). For instance, this is an example of first line I got when reading the file (value for v):
{ "id": 1, "name": "Explosives", "category_id": 1, "average_price": 294, "is_rare": 0, "max_buy_price": 755, "max_sell_price": 1774, "min_buy_price": 99, "min_sell_price": 18, "buy_price_lower_average": 176, "sell_price_upper_average": 924, "is_non_marketable": 0, "ed_id": 128049204, "category": { "id": 1, "name": "Chemicals" } }
My actual working code is :
for v in d:
commodities_reference = []
for k, g in v.items():
if isinstance(g, dict):
dict1 = g
my_value1 = dict1.get("id")
my_value2 = dict1.get("name")
for s, i in v.items():
if not isinstance(i, dict):
commodities_reference.append(i)
commodities_reference.append(my_value1)
commodities_reference.append(my_value2)
Output wanted = All the values in same list in the same order for doing a SQL INSERT Statement afterwards (Meaning values from the nested dict must be also at the end.)
[1, 'Explosives', 1, 294, 0, 755, 1774, 99, 18, 176, 924, 0, 128049204, 1, 'Chemicals']
From a performance perspective, with SQLITE3/python 3.7, it is a catastrophe. I am looking for some advices in order to make it more efficient. I am thinking about using executemany
statement but it seems it takes tuple instead of list.