hadesfv
hadesfv

Reputation: 386

updating a csv file with dict of dicts

I am trying to write into csv file where I have keys as headers and each header is a list which I want to write under header respectively.

import csv
found_items = {'a':['a','b','c','d'],'b':['af','fr']}
with open('results.csv','a') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(found_items.keys())
    writer.writerows(zip(*found_items.values()))

I have 2 issues with it so far, first issue is if list length is not equal it will write only to the length of the least

the second issue I have is when I use 'a' to append to the file it will write underneath. What I want to do is to add more columns.

i.e the following will give me

a,b
a,af
b,fr

when I write again to the file

found_items = {'c':['1a','2b','ss']}

the output is this

a,b
a,af
b,fr
c
1a
2b
ss

Ideally the output will be 3 columns and header will contain all elements in it's list My expected Output is as follows:

a,b,c
a,af,1a
b,fr,2b
c,,ss
d,,

Upvotes: 0

Views: 37

Answers (1)

gold_cy
gold_cy

Reputation: 14236

So to fix your first issue you want both lists to be the same length when you zip them otherwise elements will be discarded. This is a simple fix as shown below by using zip_longest

import csv
from itertools import zip_longest

found_items = {'a':['a','b','c','d'],'b':['af','fr']}
with open('results.csv','a') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(found_items.keys())
    writer.writerows(zip_longest(*found_items.values()))

Now for the second part we will have to use pandas to achieve the results you want.

import pandas as pd

df = pd.read_csv('results.csv')
print(df)

   a    b
0  a   af
1  b   fr
2  c  NaN
3  d  NaN

to_write = pd.concat([df, pd.DataFrame(found_items)], axis=1)
print(to_write)

   a    b    c
0  a   af   1a
1  b   fr   2b
2  c  NaN   ss
3  d  NaN  NaN

to_write.to_csv('results.csv', index=False)

Lastly when we take a look at the file we see it appears in the format as intended.

==> cat results.csv 
a,b,c
a,af,1a
b,fr,2b
c,,ss
d,,

Upvotes: 1

Related Questions