简体   繁体   English

是否可以防止迭代器到达文件末尾?

[英]Is it possible to prevent iterator from reaching end of file?

I tried to find an answer to this question, but I guess I can't really pinpoint the source of my problem. 我试图找到该问题的答案,但我想我无法真正查明问题的根源。 If this has been answered elsewhere, I'd appreciate it if you could take a moment to explain what I'm missing. 如果您在其他地方已回答过此问题,那么请您抽出一点时间来解释我所缺少的内容,我们将不胜感激。

Basically, I thought you could do something like this: 基本上,我认为您可以执行以下操作:

ref = open(ref_file)
with ref:
    for row in ref:
         break

with ref:
    for row in ref:
        print(row)

Since I used a break statement in the first loop, I thought it'd stop the iterator, put a pause on reading the file, and then I could pick up where I left off with the second loop. 因为我在第一个循环中使用了break语句,所以我认为它会停止迭代器,暂停读取文件,然后在第二个循环中停下来的地方继续工作。 This doesn't turn out to be the case- it instead throws me an error saying I am trying to work on a closed file. 事实并非如此-相反,它向我抛出一个错误,说我正在尝试处理已关闭的文件。 I reasoned through it, and I came to the conclusion that, perhaps, the with statement is what pushes the iterator to EOF. 我经过了推理,得出结论,也许with语句是将迭代器推向EOF的原因。 So I tried this: 所以我尝试了这个:

with ref:
    for row in ref:
         break

    for row in ref:
        print(row)

This seemed to work as expected. 这似乎按预期工作。 Awesome. 真棒。 So, the next thing I tried is this: 所以,我接下来尝试的是:

with ref:
    for i in range(0,10):
        for row in vcf:
             break

        for row in vcf:
            print(row)

What I'd expect to happen here is, essentially, to print alternating rows until the range of 0,10 is exhausted. 从本质上讲,我期望的是打印交替的行,直到用尽0,10的范围为止。 Instead, I print the second row of the file, and it stops. 相反,我打印文件的第二行,然后停止。

Is there a better way to prevent an iterator from reaching EOF? 有没有更好的方法来防止迭代器到达EOF? What am I missing? 我想念什么? Am I somehow breaking out of the with statement and missing the point? 我是否以某种方式突破了with语句并错过了要点?

EDIT: 编辑:

Okay, the last example isn't exemplary of the issue I'm having. 好的,最后一个示例不是我遇到的问题的示例。

with ref:
    for i in range(0,10):
        for row in vcf:
             break

        for row in vcf:
            print(row)
            break

This works as expected... Which really leaves me thinking I'm missing something in my real code. 这可以按预期工作...确实让我觉得我的真实代码中缺少某些内容。 I'd still appreciate commentary on better ways to prevent an iterator from reaching EOF. 对于阻止迭代器到达EOF的更好方法的评论,我仍将不胜感激。

The indentation here is incorrect: it reads one line then breaks then reads the rest of the file, 10 times: 此处的缩进是不正确的:它读取一行,然后中断然后读取文件的其余部分,共10次:

with ref:
    for i in range(0,10):
        for row in vcf:
             break

        for row in vcf:
            print(row)

note that you can iterate manually using next instead of breaking out of a for loop. 请注意,您可以使用next手动进行迭代,而不必脱离for循环。 More pythonic: 更多pythonic:

for _ in range(10):
    next(vcf,None)

# unindent here
for row in vcf:
    print(row)

the None argument in next ensures that even if the file is shorter than 10 lines, it doesn't raise StopIteration but yields a default value (that you're ignoring) nextNone参数可确保即使文件少于10行,它也不会引发StopIteration而是会产生一个默认值(您忽略了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM