0

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.

SomeGuy
  • 163
  • 1
  • 16
  • 2
    `payload` is empty, so what is `payload[i]` supposed to be? – Barmar May 22 '18 at 16:48
  • Maybe something like `payload[i] = {sku: Mbody[index:(index + SKU_size*2)]}`. But then where is `payload[i].tag` supposed to come from? – Barmar May 22 '18 at 16:51
  • payload[i] would become something like payload1.sku = [1011a]. Payload1 should become whatever value is held in the Mbody[index:(index + sku_size*2)]. Mbody is a log character string. So this would end up reading something like (payloadd1.sku = [38:(38+4)]) – SomeGuy May 22 '18 at 18:23
  • What about `.tag`, where does that come from? – Barmar May 22 '18 at 19:11
  • edited - that should have said something else. – SomeGuy May 22 '18 at 19:16
  • Have you tried using [`json`](https://docs.python.org/3/library/json.html)? – berna1111 May 22 '18 at 19:19
  • I'm not sure what you mean, but this will become a json by the time the script finishes. If you have info to help that along I'd appreciate it. – SomeGuy May 22 '18 at 19:22

1 Answers1

2

In python you have to name the keys of a dictionary to interact with them.

p['a']['b']
# This is element b of p['a']

p['a'].b
# This is function b in object p['a']

Dotted notation is not available by default although some people have solutions to add that.

Ignoring those, we can make some changes to your code to start working with the usual notation.

index = 38
# This can just be a list, because i is continuous between 0 and Number_SKUs
payload = []

i = 0
while i < Number_SKUs:
    SKU_size = 2
    size_index = 4
    value_index = 1
    time_index = 4
    ms_index = 2

    # Each item is going to be a new dictionary, which we will
    # eventually add the the list of payloads
    new_payload = {}

    # Now we can create a dict entry for key 'sku' in new_payload
    new_payload['sku'] = Mbody[index:(index + SKU_size * 2)]

    # We can't do this, because tag_size is undefined
    #
    # index = index + tag_size * 2

    # We can't do this, because we have not yet added a 'tag' key 
    # to payload.
    #
    # print(payload[i]['tag'])

    # Now append new_payload to payload
    payload.append(new_payload)
    i += 1
Rob Bricheno
  • 4,467
  • 15
  • 29