[Tutor] Enumerate vs DictReader object manipulation:

Danny Yoo dyoo at hashcollision.org
Wed Feb 3 11:21:19 EST 2016


> I have a code that reads a csv file via DictReader. I ran into a peculiar
> problem. The python interpreter ignores the 2nd code. That is if I put the
> reader iterator 1st, like the code below, the enumerate code is ignored; if
> I put the enumerate code 1st, the reader code is ignored. I am curious to
> know the nature of such behavior. EKE


The csv.DictReader acts in a streaming way: once you've started
pulling from it, it won't rewind back.  That is, Python isn't ignoring
your commands: it's following them very precisely.  The only problem
is that in your second loop, the reader is already exhausted: the
second call to enumerate will not rewind it.  The reason that it works
in a streaming way is fundamentally because it's reading from a file,
and files act in a streaming way to accommodate situations where the
content is too large to hold all at once in memory.


You have a few options.  If the file is itself not too large, you can
keep the contents of the DictReader as an explicit list:

    rows = list(csv.DictReader(MyFile))
    ...

after which you've got the rows held all at once, and can iterate
through those rows at will.

If the file is large, you can effectively rewind by reopening the file
and the reader again.

#################################
MyFile = ...  # opening up MyFIle
reader = csv.DictReader(MyFile)
for row in reader: ...

MyFile = ... # opening up MyFile again
reader = csv.DictReader(MyFile)
for row in reader: ...
#################################


More information about the Tutor mailing list