How to stop iteration with __iter__() ?

Jon Clements joncle at googlemail.com
Tue Aug 19 07:57:59 EDT 2008


On Aug 19, 12:39 pm, ssecorp <circularf... at gmail.com> wrote:
> I want a parse a file of the format:
> movieId
> customerid, grade, date
> customerid, grade, date
> customerid, grade, date
> etc.
>
> so I could do with open file as reviews and then for line in reviews.
>
> but first I want to take out the movie id so I use an iterator.
>
> then i want to iterate through all the rows, but how can I do:
> while movie_iter != None:
>
> because that doesn't work, itraises an exception, StopItreation, which
> according to the documentation it should. But catching an exception
> can't be the standard way to stop iterating right?

Given your example, why not just open the file and call .next() on it
to discard the first row, then iterate using for as usual?

hth
Jon



More information about the Python-list mailing list