Message309904
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. |
|
Date |
User |
Action |
Args |
2018-01-14 00:41:44 | bendotc | set | recipients:
+ bendotc |
2018-01-14 00:41:44 | bendotc | set | messageid: <1515890504.43.0.467229070634.issue32547@psf.upfronthosting.co.za> |
2018-01-14 00:41:44 | bendotc | link | issue32547 messages |
2018-01-14 00:41:43 | bendotc | create | |
|