writing header in csv python with DictWriter

Posted by user248237 on Stack Overflow See other posts from Stack Overflow or by user248237
Published on 2010-06-05T20:41:26Z Indexed on 2010/06/05 21:12 UTC
Read the original article Hit count: 305

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about parsing