Writing a csv file from a dictionary list in python

I am new to Python and I am trying to save a list of dictionaries in a csv file.

I want to write my list in csv file using pipe character as separator

My list:

[{'COAST': {'Max_Load': 18779, 'Month': 8, 'Day': 13, 'Hour': 17, 'Year': 2013}}, 
 {'EAST': {'Max_Load': 2380, 'Month': 8, 'Day': 5, 'Hour': 17, 'Year': 2013}}, 
 {'FAR_WEST': {'Max_Load': 2281, 'Month': 6, 'Day': 26, 'Hour': 17, 'Year': 2013}}, 
 {'NORTH': {'Max_Load': 1544, 'Month': 8, 'Day': 7, 'Hour': 17, 'Year': 2013}}, 
 {'NORTH_C': {'Max_Load': 24415, 'Month': 8, 'Day': 7, 'Hour': 18, 'Year': 2013}}, 
 {'SOUTHERN': {'Max_Load': 5494.157645, 'Month': 8, 'Day': 8, 'Hour': 16, 'Year': 2013}}, 
 {'SOUTH_C': {'Max_Load': 11433, 'Month': 8, 'Day': 8, 'Hour': 18, 'Year': 2013}}, 
 {'WEST': {'Max_Load': 1862, 'Month': 8, 'Day': 7, 'Hour': 17, 'Year': 2013}}, 
 {'ERCOT': {'Max_Load': 67595, 'Month': 8, 'Day': 7, 'Hour': 17, 'Year': 2013}}]

      

and my last csv should look like this:

Station | Year | Month | Day | Hour | Maximum loading

COAST | 2013 | 01 | 01 | 10 | 12345

EAST | 2013 | 01 | 01 | 10 | 12345

FAR_WEST | 2013 | 01 | 01 | 10 | 12345

NORTH | 2013 | 01 | 01 | 10 | 12345 NORTH_C | 2013 | 01 | 01 | 10 | 12345

SOUTH | 2013 | 01 | 01 | 10 | 12345

SOUTH_C | 2013 | 01 | 01 | 10 | 12345

WEST | 2013 | 01 | 01 | 10 | 12345

I am trying to write a save_file function to save a list in a csv file as above, but I am stuck on how to get the keys of a dictionary and write them on both rows ("COAST") and column ('Year', 'Month', 'Date', 'Hour', 'Max Load').

def save_file(data, filename):
for i in range(len(data)):
    for k,v in data[i].items():
        print k
        for k1,v1 in v.items():
            print k1,v1


with open(filename,'wb') as f:
    fieldnames = ["Station","Year", "Month", "Date", "Hour", "Max Load"]
    wr = csv.writer(f, delimiter="|", fieldnames = fieldnames)
    wr.writeheader()

      

ANY help would be much appreciated.

+3


source to share


2 answers


You can use csv.DictWriter

to write a dictionary in a file csv

:



list_of_dict=[{'COAST': {'Max_Load': 18779, 'Month': 8, 'Day': 13, 'Hour': 17, 'Year': 2013}}, 
 {'EAST': {'Max_Load': 2380, 'Month': 8, 'Day': 5, 'Hour': 17, 'Year': 2013}}, 
 {'FAR_WEST': {'Max_Load': 2281, 'Month': 6, 'Day': 26, 'Hour': 17, 'Year': 2013}}, 
 {'NORTH': {'Max_Load': 1544, 'Month': 8, 'Day': 7, 'Hour': 17, 'Year': 2013}}, 
 {'NORTH_C': {'Max_Load': 24415, 'Month': 8, 'Day': 7, 'Hour': 18, 'Year': 2013}}, 
 {'SOUTHERN': {'Max_Load': 5494.157645, 'Month': 8, 'Day': 8, 'Hour': 16, 'Year': 2013}}, 
 {'SOUTH_C': {'Max_Load': 11433, 'Month': 8, 'Day': 8, 'Hour': 18, 'Year': 2013}}, 
 {'WEST': {'Max_Load': 1862, 'Month': 8, 'Day': 7, 'Hour': 17, 'Year': 2013}}, 
 {'ERCOT': {'Max_Load': 67595, 'Month': 8, 'Day': 7, 'Hour': 17, 'Year': 2013}}]

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ('Station','Year', 'Month', 'Day', 'Hour', 'Max_Load')
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames,delimiter='|')
    writer.writeheader()
    for d in list_of_dict:
          k,v=d.items()[0]
          v['Station']=k
          writer.writerow(v)

      

+3


source


Here you go:

lst=...

subkeys=["Year","Month","Day","Hour","Max_Load"]
with open("test.csv", "w") as f:
    f.write("Station|Year|Month|Day|Hour|Max Load\n\n")
    for elem in lst:
        for key in elem.keys():
            f.write(key)
            f.write("|")
            for k, subkey in enumerate(subkeys):
                f.write(str(elem[key][subkey]))
                if k < len(subkeys)-1: f.write("|")
            f.write("\n\n")

      



+2


source







All Articles