writing header in csv python with DictWriter
- by user248237
assume I have a csv.DictReader object and I want to write it out as a csv file. How can I do this?
I thought of the following:
dr = csv.DictReader(open(f), delimiter='\t')
# process my dr object
# ...
# write out object
output = csv.DictWriter(open(f2, 'w'), delimiter='\t')
for item in dr:
output.writerow(item)
Is that the best way?
More importantly, how can I make it so a header is written out too, in this case the object "dr"s .fieldnames property?
thanks.