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.

7
  • 2
    payload is empty, so what is payload[i] supposed to be?
    – Barmar
    Commented May 22, 2018 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
    Commented May 22, 2018 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
    Commented May 22, 2018 at 18:23
  • What about .tag, where does that come from?
    – Barmar
    Commented May 22, 2018 at 19:11
  • edited - that should have said something else.
    – SomeGuy
    Commented May 22, 2018 at 19:16

1 Answer 1

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.