CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

From dictionary of lists to csv

Hi everyone!!

I have a dictionary organized like this:

dict = {
year: {name: [some values],
       duration: [some values],
       loudness: [some values]
       milliseconds: [some values]
},
year_2: {name: [some values],
         duration: [some values],
         loudness: [some values]
         milliseconds: [some values]
}

How can I do to convert it into a csv so that I have as headers:

name, duration, loudness, milliseconds

and as values, the values present in the lists?

I tried like this:

for year in tracks_features.keys():
    with open("test.csv", mode='w', newline='') as outfile:
        writer = csv.writer(outfile)
        writer.writerow(tracks_features[year].keys())
        writer.writerows(zip(*tracks_features[year].values()))

But the output is a csv with just the values of the second year.
Thank you

I’m sorry, I just solved the problem.
You can close the post.