csv.DictWriter.write_header()
John Machin
sjmachin at lexicon.net
Thu Aug 13 07:58:33 EDT 2009
On Aug 13, 1:15 pm, Alan G Isaac <alan.is... at gmail.com> wrote:
> > On Aug 13, 6:45 am, Alan G Isaac <alan.is... at gmail.com> wrote:
> >> Given a csv.DictWriter instance `dw`
> >> I think it would be nice to be able to
> >> say dw.write_header()
> >> instead of
> >> dw.writer.writerow(dw.fieldnames)
>
> >> Good idea?
>
> On 8/12/2009 10:24 PM John Machin apparently wrote:
>
> > Yes, it's a brilliant idea. All you have to do is insert the following
> > lines in your code after importing csv:
>
> > csv.DictWriter.write_header = (
> > lambda dw: dw.writer.writerow(dw.fieldnames)
> > )
>
> I do not understand the reason for your silly, sarcastic response.
Duck typing: ask a silly question, get a silly answer.
> I already showed you how to achieve the result,
What result? You said (in effect) "I think it would be nice to be able
to write A instead of B". You didn't show any way of setting things up
so that one could write A instead of B.
I showed one way. Other possibilities are a 2-line patch to the users
own csv.py, and submitting a patch to the dev team. The second is
kludgy, and there could be a wait until e.g. 2.7 before the third
happens. So back to the first way; everybody should have a utility
module that they import to do little fixes like that ... here is a
slightly more robust version:
import csv
try:
csv.DictWriter.write_header
except AttributeError:
csv.DictWriter.write_header = (
lambda dw: dw.writer.writerow(dw.fieldnames)
)
> so obviously
> I am not just asking for my own personal gain.
???
> I take it you do not realize that many users do not see
> a simple way to get this often needed functionality.
No, I don't. How did you come to that realisation? AFAICT they don't
ask in this forum or on StackOverflow.
> (For example, see the gymnatics suggested by Shai Vaingast
> in his otherwise excellent and useful book.)
I can imagine that one might (without reading the source) make do with
the published APIs:
dw = csv.DictWriter(open(...), my_field_names
dw.writerow(dict((fn, fn) for fn in my_field_names))
> And indeed,
> there is no reason to expect them to.
Indeed not, as for somebody who otherwise sees utility in the
DictWriter class not having a writer_headers method is a glaring
deficiency.
Note that there is a difference between "expecting C to do X" and
"expecting C to be able to do X". I really hope you don't mean that
people should not be encouraged to be able to write a trivial class
like csv.DictWriter and add trivial functionality themselves ... or to
be able to just write the required functionality inline:
wtr = csv.writer(open(...), ...)
wtr.writerow(my_field_names)
for adict in source_of_dicts():
wtr.writerow(adict[fn] for fn in my_field_names)
or even:
for obj in source_of_objects():
wtr.writerow(getattr(obj, fn) for fn in my_field_names)
> So my question was, would this improve the class from
> a usability perspective?
Of course.
> Do you have a useful response?
See above. Also, the csv module appears to be in two minds about
underscores in attribute names (field_size_limit, skipinitialspace,
writerow, writerows) -- it may cause less confusion if it were called
writeheader (consistent with writerow).
More information about the Python-list
mailing list