I am new to Python and tried all things I could think of and could not find a solution to this. I have a list that contains as the last of its items one dictionary, with different number of keys, that looks like.
l = [('Apple', 1, 2, {'gala': (2, 1.0)}),
('Grape ', 2, 4, {'malbec': (4, 0.25), 'merlot': (4, 0.75)}),
('Pear', 4, 5, {'anjou': (5, 0.2), 'bartlet': (5, 0.4), 'seckel': (5, 0.2)}),
('Berry', 5, 5, {'blueberry': (5, 0.2), 'blackberry': (5, 0.2), 'straw': (5, 0.2)})]
When I try to write a .csv file from the current list, I used:
test_file = ()
length = len(l[0])
with open('test1.csv', 'w', encoding = 'utf-8') as test_file:
csv_writer = csv.writer(test_file, delimiter=',')
for y in range(length):
csv_writer.writerow([x[y] for x in l])
It makes the last element on the list, the dictionary, to be only one string in the output file:
Apple 1 2 {'gala': (2, 1.0)}
Grape 2 4 {'malbec': (4, 0.25), 'merlot': (4, 0.75)}
Pear 4 5 {'anjou': (5, 0.2), 'bartlet': (5, 0.4), 'seckel': (5, 0.2), 'bosc': (5, 0.2)}
Berry 5 5 {'blueberry': (5, 0.2), 'blackberry': (5, 0.2), 'straw': (5, 0.2)}
Which renders impossible to to any operations with the values inside the last item.
I tried to flatten the nested dictionary so I would get just a plain list, but the outcome does not preserve the relationship between items. What I need is to split the dictionary and have an output that would look somewhat like this:
Apple 1 2 gala 2 1.0
Grape 2 4 malbec 4 0.25
merlot 4 0.75
Pear 4 5 anjou 5 0.2
bartlet 5 0.4
seckel 5 0.2
bosc 5 0.2
Berry 5 5 blueberry 5 0.2
blackberry 5 0.2
straw 5 0.2
I mean somewhat like this because I am not committed to this format, but to the idea that the hierarchical relation of the dictionary will not be lost in the output file. Is there a way to do it? I am really new to python and appreciate any help. Thanks!