Skip to content
Advertisement

Python: List of dictionaries – using keys as headers & rows are the values (Naive way)

I’m currently tasked with creating a CSV file from a list of dictionaries where the headers are the dic keys and the rows the dic values

Ex.

JavaScript

Output would be a CSV File:

JavaScript

We’re not allowed to use dictwriter/csv/pandas and have to do it the naïve way.

I currently have the keys gathered, but am struggling with trying to split the values so that instead of printing out all the values in the same line it writes the 2nd dictionary values in a new line:

My getting values code:

JavaScript

My current file output:

JavaScript

Advertisement

Answer

JavaScript

At this point, final is a comma delimited string:

JavaScript

Then you can just write that to disk:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement