On Sep 18, 2013, at 5:07 AM, nikhil Pandey <nikhilpande...@gmail.com> wrote:

> On Wednesday, September 18, 2013 4:51:51 PM UTC+5:30, Chris Angelico wrote:
>> On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey <nikhilpande...@gmail.com> 
>> wrote:
>> 
>>> hi,
>> 
>>> I want to iterate over the lines of a file and when i find certain lines, i 
>>> need another loop starting from the next of that "CERTAIN" line till a few 
>>> (say 20) lines later.
>> 
>>> so, basically i need two pointers to lines (one for outer loop(for each 
>>> line in file)) and one for inner loop. How can i do that in python?
>> 
>>> please help. I am stuck up on this.
>> 
>> 
>> 
>> After the inner loop finishes, do you want to go back to where the
>> 
>> outer loop left off, or should the outer loop continue from the point
>> 
>> where the inner loop stopped? In other words, do you want to locate
>> 
>> overlapping sections, or not? Both are possible, but the solutions
>> 
>> will look somewhat different.
>> 
>> 
>> 
>> ChrisA
> 
> Hi Chris,
> After the inner loop finishes, I want to go back to the next line from where 
> the outer loop was left i.e the lines of the inner loop will be traversed 
> again in the outer loop.
> 1>>I iterate over lines of the file
> 2>> when i find a match in a certain line, i start another loop till some 
> condition is met in the subsequent lines
> 3>> then i come back to where i left and repeat 1(ideally i want to delete 
> that line in inner loop where that condition is met, but even if it is not 
> deleted, its OK)


Just curious, do you really need two loops and file handles? Without better 
details about what you're really doing, but as you've provided more detail, it 
seems to me that just iterating the lines of the file, and using a latch 
boolean to indicate when you should do additional processing on lines might be 
easier. I modified Chris's example input to look like:

alpha
*beta
gamma+
delta
epsilon
zeta
*eta
kappa
tau
pi+
omicron

And then shot it with the following:

#!/usr/bin/env python3
with open("samplein.txt") as file:
    reversing = False
    for line in (raw.strip() for raw in file):
        if reversing:
            print('____', line[::-1], '____')
            reversing = not line.endswith('+')
        else:
            print(line)
            reversing = line.startswith('*')

Which begins reversing lines as its working through them, until a different 
condition is met.

Travis Griggs

                
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to