I have a list of nested dictionaries that looks like this:
[{'posts': {'item_1': 1,
'item_2': 8,
'item_3': 105,
'item_4': 324,
'item_5': 313, }},
{'edits': {'item_1': 1,
'item_2': 8,
'item_3': 61,
'item_4': 178,
'item_5': 163}},
{'views': {'item_1': 2345,
'item_2': 330649,
'item_3': 12920402,
'item_4': 46199102,
'item_5': 43094955}}]
I would like to write it to an excel file in this format:
+--------+-------+-------+-----------+
| | posts | edits | views |
+--------+-------+-------+-----------+
| item_1 | 1 | 1 | 2345 |
| item_2 | 8 | 8 | 330649 |
| item_3 | 105 | 61 | 12920402 |
| item_4 | 324 | 178 | 46199102 |
| item_5 | 313 | 163 | 430949955 |
+--------+-------+-------+-----------+
I am using the xlsxwriter library and trying the following and variations on the following without success:
for item in data:
for col_name, data in item.iteritems():
col += 1
worksheet.write(row, col, col_name)
for row_name, row_data in data.iteritems():
col += 1
worksheet.write(row, col, row_name)
worksheet.write(row + 1, col, row_data)
I'm wondering if it makes sense to rework my nested dictionary object or is it possible to write to excel in it's current form?
When I say without much success i mean, that I can get it to write certain thigns to the excel file, like column names or row or the data, but I am unable to get it to write like pictured above. I'm not getting errors, I suspect i jsut don't know how to unpack this object properly to loop through it. In the code above, I am given a combination of row and column names on row 1 and all of the values on row 2.
My output for the code above is:
+--+-------+--------+--------+--------+--------+--------+-------+--------+--------+--------+--------+--------+-------+----------+----------+--------+----------+--------+
| | posts | item_4 | item_5 | item_2 | item_3 | item_1 | edits | item_4 | item_5 | item_2 | item_3 | item_1 | views | item_4 | item_5 | item_2 | item_3 | item_1 |
+--+-------+--------+--------+--------+--------+--------+-------+--------+--------+--------+--------+--------+-------+----------+----------+--------+----------+--------+
| | | 324 | 313 | 8 | 105 | 1 | | 178 | 163 | 8 | 61 | 1 | | 46199102 | 43094955 | 330649 | 12920402 | 2345 |
+--+-------+--------+--------+--------+--------+--------+-------+--------+--------+--------+--------+--------+-------+----------+----------+--------+----------+--------+
{'item1': {'posts': 8, 'edits': 1, ...}, ...})?