[ python-Feature Requests-1537721 ] csv module: add header row to DictWriter

SourceForge.net noreply at sourceforge.net
Thu Aug 10 00:20:01 CEST 2006


Feature Requests item #1537721, was opened at 2006-08-10 10:20
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1537721&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: ed_abraham (ed_abraham)
Assigned to: Nobody/Anonymous (nobody)
Summary: csv module: add header row to DictWriter

Initial Comment:
I use the DictWriter class from the csv module, and
have to manually write the header row. A mindless chore
which I would like to see eliminated. Can we have a
writeheader method added to the class? Something like
the following:

def writeheader(self, headernames = {}):
    """Write a header row"""
    if not headernames:
        headernames = dict(zip(self.fieldnames,
self.fieldnames))
        self.writerow(headernames)

This would let you either use the fieldnames directly,
or supply your own pretty header names. 

Would be nice to have another keyword argument to
DictWriter, 'header = False'. If header was true, then
the __init__ method could call writeheader(). 


At the moment I have to write things like

fields = ['a','b','c']
w = csv.DictWriter(fid, fields)
w.writerow(dict(zip(fields, fields)))
for row in rows:
    w.writerow(row)

The proposed changes would let me write the simpler

w = csv.DictWriter(fid, ['a','b','c'], header = True)
for row in rows:
    w.writerow(row)


A problem is that including a new keyword argument
would break code which used position to fill the
keyword arguments and to supply arguments through *args
to the writer class.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1537721&group_id=5470


More information about the Python-bugs-list mailing list