[Csv] DictReader/DictWriter behavior question

Skip Montanaro skip at pobox.com
Mon Feb 10 16:41:13 CET 2003


I have DictReader set up to handle short or long rows in a reasonable
fashion.  If the number of fields in the input row is more than the number
len of the "fieldnames" list passed to the constructor, an extra field, keyed
by the optional "rest" argument gathers the remaining data.  For example:

    >>> rdr = csv.DictReader (["a,b,c,d,e\r\n"], fieldnames="1 2 3".split())
    >>> rdr.next()
    {'1': 'a', None: ['d', 'e'], '3': 'c', '2': 'b'}
    >>> rdr = csv.DictReader (["a,b,c,d,e\r\n"], fieldnames="1 2 3".split(), rest="foo")
    >>> rdr.next()
    {'1': 'a', '3': 'c', '2': 'b', 'foo': ['d', 'e']}

Similarly, if the row is short:

    >>> rdr = csv.DictReader (["a,b,c\r\n"], fieldnames="1 2 3 4 5 6".split(), restval="dflt")
    >>> rdr.next()
    {'1': 'a', '3': 'c', '2': 'b', '5': 'dflt', '4': 'dflt', '6': 'dflt'}

(I'm about to change the "rest" parameter to "restkey".)

My problem is the DictWriter.  It uses a similar mechanism to map dicts to
output rows:

    >>> f = StringIO.StringIO()
    >>> wrtr = csv.DictWriter(f, fieldnames="1 2 3".split())
    >>> wrtr.writerow({"1":30,"2":20,"3":10})
    >>> f.getvalue()
    '30,20,10\r\n'

When writing though, I face the dilemma of what to do if the dictionary
being written has one or more keys which don't appear in the fieldnames
list.  I can silently ignore them (that's the current behavior), I can raise
an exception, or I can give the user control.  There's no way to actually
write that data because you have no obvious way to order those values.  (I
could do something hokey like write out the key and the value somehow.)

What do you think is the best behavior, ignore values or raise an exception?
Or do you have other ideas?

Skip




More information about the Csv mailing list