I would like to write a list of Python dictionaries into a file. However, I need the dictionaries (and lists within) to remain dictionaries, i.e. when I load the file for processing I want them to use dictionaries and not have to work with strings.
Here is my sample code which write the data as strings, is there a way to retain the origin Python data structures (in real code the list data has hundreds of dictionaries, each of which may have hundreds of lists as values). I cannot simple pickle the data, for a number of reasons (one of which is the file needs to be human readable).
import csv
import pandas as pd
def write_csv_file(data, iteration):
with open('%s.csv' % 'name', 'wb') as data_csv:
writer_data = csv.writer(data_csv, delimiter=',')
for d in data:
writer_data.writerow([iteration] + [d])
data = [{'a':1, 'b':2}, {'e':[1], 'f':[2,10]}]
iteration = 1
write_csv_file(data, iteration)
At the moment I read the data file using pandas in the following manner to process the data.
d = pd.read_csv('name.csv')
d = pd.DataFrame(d)