0

I'm trying to parse multilevel JSON to Excel using xlwt. JSON format is like this:

{
     "A": {
              "A1": 0, "A2": 0
          },
     "B": {
              "B1": 0, "B2": 0
          }
}

etc.

I've tried following (obviously after opening and loading JSON into Python dictionary):

for k in data:
    for l in data.keys():
        for j in l.keys():
            o = []
            o = j
            ws.write(0, l.keys().index(j)+1, l[j])
        ws.write(data.keys().index(k)+1, l.keys().index(o)+1, o)
    ws.write(data.keys().index(k)+1, 0, k[l])

But I receive "unicode" object doesn't have attribute "keys"

1
  • Please edit your question and add more information. Which line produces the error? Also, how are you creating the dictionary? Commented Apr 11, 2017 at 16:57

2 Answers 2

1

You could use the package StyleFrame.

First convert the data to format that can be used in StyleFrame object and then just use the StyleFrame object.

from StyleFrame import StyleFrame

data = {'A': {'A1': 0, 'A2': 0}, 'B': {'B1': 0, 'B2': 0}}
formated_data = {col: [data[col][row] for row in sorted(data[col])] for col in data}

StyleFrame(data).to_excel('output.xlsx',
                          row_to_add_filters=0,
                          columns_and_rows_to_freeze='A2').save()
Sign up to request clarification or add additional context in comments.

Comments

0

I just did a little testing. See the test case below to highlight how to loop your multilevel dictionary. I can't tell exactly the logic of what you are writing, so I will give a generalized answer:

In [44]: data
Out[44]: {'A': {'A1': 0, 'A2': 0}, 'B': {'B1': 0, 'B2': 0}}

In [45]: for i in data:
    ...:     print(data[i])
    ...:     
{'A1': 0, 'A2': 0}
{'B1': 0, 'B2': 0}

#You can see that `i` represents  the key, so you access the value paired with that key by doing `data[i]`. In this case, that value is another dictionary.

In [46]: for i in data:
    ...:     for j in data[i]:
    ...:         print(data[i][j])
    ...:         
0
0
0
0

#This is far less illustrative than my contrived example, but `j` represents the key in the dictionary `data[i]`. So this second loop goes through and prints each value of the nested dictionaries. 

Let me know if this isn't clear.

3 Comments

I didn't become a programmer yesterday, yet I have no idea how this code is supposed to answer the question. Maybe I'm just too stupid, but some explanation of what's going on here would be helpful.
It shows generally how to loop through a multilevel dictionary, which appears to be what the question is asking. The structure of the sample dict is exactly the same structure as OP. I can change it to be the exact same if that is more clear. I'll try to clarify a bit in the answer.
Well, that's one of the most basic things in Python. He's trying to put it in a spread sheet, but he didn't specify how exactly he is planning to do that...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.