[Tutor] Problem iterating over csv.DictReader

Matthew Williams mhw at doctors.org.uk
Mon Apr 26 15:00:24 CEST 2010

Dear All,

I have noticed this odd behaviour in the CSV DictReader 
Class, and at a loss to understand/ get around it.

The aim is to read in a CSV file, and then iterate over 
the lines. The problem (seems) to be that once you have 
iterated over it once, you can't do it again.

I don't know if this is me (and it may well be) but it 
seems to be a recurrent issue, and means that a 
csv.DictReader doesn't behave in the same way as a normal 
dict object.

Code to confirm/ replicate below.

What I'm looking for is a way to explicity reset the 
iterator, to tell it to go back to the beginning.

Any ideas (or pointing out I am a moron) very welcome.


Python 2.6.3 (r253:75183, Oct 11 2009, 18:26:07)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu4)] on linux2

import csv
fname = "insert your csv file here.csv"
inr = csv.DictReader(open(fname, 'r'), delimiter = ',', 
quotechar = '"')

for r in inr:
     print r

<Get your rows here>

for r in inr:
     print r


If I reload the file, I can solve the issue

More information about the Tutor mailing list