Using python 'with' with iterators?

I am using Python 2.5. I am trying to use this expression with.

from __future__ import with_statement a = [] with open('exampletxt.txt','r') as f: while True: a.append(f.next().strip().split()) print a 

The contents of "exampletxt.txt" is simple:

 a b 

In this case, I get the error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/tmp/python-7036sVf.py", line 5, in <module> a.append(f.next().strip().split()) StopIteration 

And if I replaced f.next() with f.read() , it seems to end up in an infinite loop.

I wonder if I should write a decorator class that takes an iterator object as an argument and define an __exit__ method for it?

I know that it is more pythonic to use for-loop for iterators, but I wanted to implement a while loop in the generator that called the for ... loop something

 def g(f): while True: x = f.next() if test1(x): a = x elif test2(x): b = f.next() yield [a,x,b] a = [] with open(filename) as f: for x in g(f): a.append(x) 
+4
source share
4 answers

Boosting StopIteration is what an iterator does when it comes to the end. Usually, the for statement captures it silently and continues the else clause, but if it is executed manually as in your case, then the code should be prepared to handle the exception itself.

+4
source

Your while loop does not end, but the file makes it throw a StopIteration exception if there is no iteration.

+1
source

You do not have any termination conditions in any of your while loops, so you keep returning until you get a StopIteration exception that you are not handling.

+1
source

You can always rewrite while-with-explicit-next loops. When you have an explicit next , you just look forward at one token.

As a rule, loops of this form can be rewritten.

 def g(f): while True: x = f.next() if test1(x): a = x elif test2(x): b = f.next() yield [a,x,b] 

You can always replace look-ahead next by buffering the value.

 def g(f): prev, a = None, None for x in f: if test2(prev) yield [ a, prev, x ] elif test1(x): a = x prev= x 
+1
source

Source: https://habr.com/ru/post/1303886/


All Articles