I'm writing a script and iterating over a list to turn it into a JSON. My code:
Mbody
is the name of the list I'm pulling info from.
index = 38
payload = {}
i = 0
while i < Number_SKUs:
SKU_size = 2
size_index = 4
value_index = 1
time_index = 4
ms_index = 2
payload[i].sku = Mbody[index:(index + SKU_size*2)]
index = index + SKU_size*2
print(payload[i].sku)
i+=1
For some reason, this is resulting in
Traceback (most recent call last): File "parser.py", line 101, in <module> payload[i].sku = Mbody[index:(index + SKU_size*2)] KeyError: 0
I have searched on stack exchange and found several similar questions, but none of which answer mine. What's the best way to iterate over a list and parse the skus? This will become a JSON eventually, but I could work with it in an array for the time being and use a dict to put it into a JSON later.
payload
is empty, so what ispayload[i]
supposed to be?payload[i] = {sku: Mbody[index:(index + SKU_size*2)]}
. But then where ispayload[i].tag
supposed to come from?.tag
, where does that come from?