Can I reset an iterator / generator in Python? I am using DictReader and would like to reset it (from the csv module) to the beginning of the file.
I see many answers suggesting itertools.tee, but that's ignoring one crucial warning in the docs for it:
Basically,
As several answers rightly remarked, in the specific case of |
|||||
|
Only if the underlying type provides a mechanism for doing so (e.g. |
|||
|
No. Python's iterator protocol is very simple, and only provides one single method ( The common pattern is to instead create a new iterator using the same procedure again. If you want to "save off" an iterator so that you can go back to its beginning, you may also fork the iterator by using |
|||||||||||||
|
If you have a csv file named 'blah.csv' That looks like
you know that you can open the file for reading, and create a DictReader with
Then, you will be able to get the next line with
using it again will produce
However, at this point if you use
again. This seems to be the functionality you're looking for. I'm sure there are some tricks associated with this approach that I'm not aware of however. @Brian suggested simply creating another DictReader. This won't work if you're first reader is half way through reading the file, as your new reader will have unexpected keys and values from wherever you are in the file. |
|||||||||||||
|
While there is no iterator reset, the "itertools" module from python 2.6 (and later) has some utilities that can help there. One of then is the "tee" which can make multiple copies of an iterator, and cache the results of the one running ahead, so that these results are used on the copies. I will seve your purposes:
|
|||
|
There's a bug in using .seek(0) as advocated by Alex Martelli and Wilduck above, namely that the next call to .next() will give you a dictionary of your header row in the form of {key1:key1, key2:key2, ...}. The work around is to follow file.seek(0) with a call to reader.next() to get rid of the header row. So your code would look something like this:
|
|||
|
Yes, if you use
|
|||||
|
For DictReader:
For DictWriter:
|
|||
|