So far all I am able to do within the function is store all the data twice over.
import csv
def csvWriter(filename, records):
header = []
for i in records:
for v in i:
header.append(v)
test = open(filename,'w')
dict_wr = csv.DictWriter(test,header)
dict_wr.writerow(dict(zip(header,header)))
for i in records:
dict_wr.writerow(dict(zip(header,i.values())))
test.close()
return '%d records processed.' % len(records)
File contains:
a,b,a,b
1,2,1,2
3,4,3,4
I believe I found the problem, inside the for loop, I’m having trouble creating the proper header.
It looks like your records in this example are 2 dictionaries with the same keys (‘a’ and ‘b’). You can fix this by getting the header from the first dictionary:
However, if your other records have unique key values, you’ll probably want to include them in the header:
Finally, if you have repeated values in the header, and you have DictWriter write a row, it will repeat entries for each duplicated header value. For example,