[New-bugs-announce] [issue20004] csv.DictReader classic class has a property with setter

Peter Otten report at bugs.python.org
Tue Dec 17 12:09:53 CET 2013


New submission from Peter Otten:

I ran into this when trying to trigger rereading the column names with

$ cat tmp.csv
alpha,beta
1,2
gamma,delta,epsilon
3,4,5
$ python
Python 2.7.2+ (default, Jul 20 2012, 22:15:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> with open("tmp.csv") as f:
...     reader = csv.DictReader(f)
...     for i in range(2):
...             print next(reader)
...             reader.fieldnames = None
... 
{'alpha': '1', 'beta': '2'}
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/lib/python2.7/csv.py", line 112, in next
    d = dict(zip(self.fieldnames, row))
TypeError: zip argument #1 must support iteration

reader = csv.DictReader(...)
...
reader.fieldnames = None

I think the easiest fix would be to have it inherit from object:

>>> class DictReader(csv.DictReader, object): pass
... 
>>> with open("tmp.csv") as f:
...     reader = DictReader(f)
...     for i in range(2):
...             print next(reader)
...             reader.fieldnames = None
... 
{'alpha': '1', 'beta': '2'}
{'3': 'gamma', '5': 'epsilon', '4': 'delta'}

----------
messages: 206418
nosy: peter.otten
priority: normal
severity: normal
status: open
title: csv.DictReader classic class has a property with setter
versions: Python 2.7

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


More information about the New-bugs-announce mailing list