如何使用__iter __()停止迭代? [英] How to stop iteration with __iter__() ?

查看:107
本文介绍了如何使用__iter __()停止迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个解析格式的文件:

movieId

customerid,年级,日期

customerid,年级,日期

customerid,等级,日期




所以我可以用open文件作为评论,然后用于评论。


但首先我要取出电影ID,所以我使用迭代器。


然后我想遍历所有行,但是我该怎么办:

而movie_iter!=无:


因为那不起作用,引发异常,StopItreation,这

根据它应该的文件。但是捕获异常

不能成为停止迭代的标准方法吗?

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?

推荐答案

8月19日, 7:39 * am,ssecorp< circularf ... @ gmail.comwrote:
On Aug 19, 7:39*am, ssecorp <circularf...@gmail.comwrote:

我想解析一个格式文件:

movieId

customerid,等级,日期

customerid,等级,日期

customerid,等级,日期

等等


所以我可以用开放文件作为评论然后在评论中排队。


但首先我要拿出来电影ID所以我使用了一个迭代器。


然后我想遍历所有行,但我该怎么做:

而movie_iter!=无:


,因为它不起作用,引发异常,StopItreation,根据它应该的文档,它是
。但是捕获异常

不能成为停止迭代的标准方法吗?
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?



编号如果你要定义迭代器,那么迭代器在完成时会引发

StopIteration。

通常捕获并不例外;你使用for循环进行迭代,并且for循环的机制使用StopIteration异常告诉它何时
break。类似文件的对象本身就是一个行迭代器,所以:


for file in file_obj:

if matches_some_pattern(line):

产量线


然后,使用它:


file_obj = open(''/ path / to / some / file.txt'')

尝试:

在review_iter(file_obj)中查看:

do_something(评论)

终于:

file_obj.close()

No. If you are defining the iterator, then the iterator raises
StopIteration when it is complete. It is not an exception you
normally catch; you use a for loop to iterate, and the machinery of
the for loop uses the StopIteration exception to tell it when to
break. A file-like object is itself a line iterator, so:

def review_iter(file_obj):
for line in file_obj:
if matches_some_pattern(line):
yield line

Then, to use it:

file_obj = open(''/path/to/some/file.txt'')
try:
for review in review_iter(file_obj):
do_something(review)
finally:
file_obj.close()


8月19日,12:39 pm,ssecorp< circularf ... @ gmail.comwrote:
On Aug 19, 12:39 pm, ssecorp <circularf...@gmail.comwrote:

我想解析一个格式文件:

movieId

customerid,等级,日期

customerid,等级,日期

customerid,等级,日期

等等/>

所以我可以用open文件作为评论,然后用于评论。


但首先我想取出电影ID所以我使用迭代器。


然后我想遍历所有行,但是ho我可以这样做:

而movie_iter!=无:


因为那不起作用,引发异常,StopItreation,

根据它应该的文件。但是捕获异常

不能成为停止迭代的标准方法吗?
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?



给出你的例子,为什么不打开文件并调用它上面的.next()

丢弃第一行,然后迭代像往常一样使用?


hth

Jon

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


2008年8月19日星期二7:39 PM,ssecorp< ci ********** @ gmail.comwrote:
On Tue, Aug 19, 2008 at 7:39 PM, ssecorp <ci**********@gmail.comwrote:

我想解析一个格式文件:

movieId

customerid,年级,日期

customerid,年级,日期

customerid,年级,日期




所以我可以用开放文件作为评论然后在评论中排队。


但首先我想取出电影ID,所以我使用了一个迭代器。


然后我想遍历所有行,但我该怎么做:

虽然movie_iter!=没有:


因为那不起作用,它会引发异常,StopItreation,根据它应该提供的文件,它是
。但是捕获异常

不能成为停止迭代的标准方法吗?
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?



你可以用于循环:

for movie_iter:

...


-

最诚挚的问候,

Leo Jay

you can use for loop:
for line in movie_iter:
...

--
Best Regards,
Leo Jay


这篇关于如何使用__iter __()停止迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆