csv.DictWriter.write_header()

A

Alan G Isaac

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?

Alan Isaac
 
J

John Machin

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?

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)
)
 
A

Alan G Isaac

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.
I already showed you how to achieve the result, 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.
(For example, see the gymnatics suggested by Shai Vaingast
in his otherwise excellent and useful book.) And indeed,
there is no reason to expect them to.

So my question was, would this improve the class from
a usability perspective? Do you have a useful response?

Alan Isaac
 
J

John Machin

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top