[New-bugs-announce] [issue19449] csv.DictWriter can't handle extra non-string fields

Tomas Grahn report at bugs.python.org
Wed Oct 30 11:59:32 CET 2013


New submission from Tomas Grahn:

When csv.DictWriter.writerow is fed a dict with extra fieldnames (and extrasaction='raise') and any of those extra fieldnames aren't strings, a TypeError-exception is thrown. To fix the issue; in csv.py, edit the line:
raise ValueError("dict contains fields not in fieldnames: " + ", ".join(wrong_fields))

to:
raise ValueError("dict contains fields not in fieldnames: " + ", ".join(repr(wrong_field) for wrong_field in wrong_fields))

Attached is a patch that fixes the problem (works in both 2.6 and 2.7, I haven't tried anything else).

Here is a simple test to demonstrate the problem:

import cStringIO, csv

sio=cStringIO.StringIO()
writer=csv.DictWriter(sio, ["foo", "bar"])
writer.writerow({1:"hello", 2:"world"})

----------
files: csv-patch.diff
keywords: patch
messages: 201727
nosy: tomas_grahn
priority: normal
severity: normal
status: open
title: csv.DictWriter can't handle extra non-string fields
type: behavior
versions: Python 2.6, Python 2.7
Added file: http://bugs.python.org/file32425/csv-patch.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19449>
_______________________________________


More information about the New-bugs-announce mailing list