This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bendotc
Recipients bendotc
Date 2018-01-14.00:41:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1515890504.43.0.467229070634.issue32547@psf.upfronthosting.co.za>
In-reply-to
Content
If I pass an iterator to csv.DictWriter as the fieldname field, then DictWriter consumes that iterator pretty quickly, emitting strange errors such as the following when trying to write the headers.

>>> import csv
>>> fields = iter(["a", "b", "c", "d"])
>>> f = open('test.csv', 'w')
>>> writer = csv.DictWriter(f, fieldnames=fields)
>>> writer.writeheader()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/csv.py", line 142, in writeheader
    self.writerow(header)
  File "/usr/lib/python3.4/csv.py", line 153, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
  File "/usr/lib/python3.4/csv.py", line 149, in _dict_to_list
    + ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: 'c', 'a'

This is because writeheader and _dict_to_list repeatedly iterate over self.fieldnames. It seems like this could be solved by making a list of fieldnames in the constructor.
History
Date User Action Args
2018-01-14 00:41:44bendotcsetrecipients: + bendotc
2018-01-14 00:41:44bendotcsetmessageid: <1515890504.43.0.467229070634.issue32547@psf.upfronthosting.co.za>
2018-01-14 00:41:44bendotclinkissue32547 messages
2018-01-14 00:41:43bendotccreate