[issue1537721] csv module: add header row to DictWriter
report at bugs.python.org
Sun Apr 19 06:10:28 CEST 2009
Matthew Iversen <teh.ivo at gmail.com> added the comment:
Skip, you were arguing in another csv issue on a NamedTupleReader that
the Reader and Writer should work in concert together.
Certainly, making this default functionality for DictWriter would
definitely make it work more in concert with DictReader.
A sample process of reading and writing might be:
1. DictReader reads in header names, reads in data
2. Pass in header names to DictWriter on init
3. DictWriter writes out modified data back to csv file (headers get
written automatically or with a method call)
4. DictReader can now reader in the csv file automatically again with
My feeling is, if DictReader can read in head names, why can't
DictWriter write them back out again? Shouldn't there be a good amount
of symmatry to their function/abilities?
My feeling of how to implement this functionality would be to include a
new init argument, say 'writeheader'. It's default would be True if you
wanted to implement this new feature by default, or False if you wanted
to keep it an option so that older scripts relying on the old
functionality won't break immmediately.
DictWriter would then write the header on the first call to writerow(s),
or perhaps also with an explicit call to writeheader() say.
I certainly am miffed by the fact that DictWriter cannot produce
(normally) a csv file that DictReader can read in automatically.
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list