Issue 1634717: csv.DictWriter: Include offending name in error message (original) (raw)
In csv.py, class DictWriter, method _dict_to_list, when rowdict contains a key that is not a known field name, a ValueError is raised, but no reference to the offending name is given. As the code iterates along the dict keys, and stops at the first unknown one, its trivial to include such information. Replace lines:
if k not in self.fieldnames:
raise ValueError, "dict contains fields not in fieldnames"
with:
if k not in self.fieldnames:
raise ValueError, "dict contains field not in fieldnames: %r" % k