Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subsequent': 0.05; 'lines,': 0.07; 'modified': 0.07; 'python3': 0.07; 'subject:file': 0.07; 'subject:two': 0.07; 'wednesday,': 0.07; 'iterate': 0.09; 'pointers': 0.09; '(say': 0.16; '20)': 0.16; 'boolean': 0.16; 'chris,': 0.16; 'deleted,': 0.16; 'doing,': 0.16; 'easier.': 0.16; 'file))': 0.16; 'file):': 0.16; 'i.e': 0.16; 'iterating': 0.16; 'loop.': 0.16; 'loops': 0.16; 'met.': 0.16; 'reversing': 0.16; 'travis': 0.16; 'alpha': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'basically': 0.19; 'file,': 0.19; 'possible,': 0.19; 'help.': 0.21; 'seems': 0.21; '>>>': 0.22; 'input': 0.22; 'example': 0.22; 'python?': 0.22; 'to:name:python- list@python.org': 0.22; 'delta': 0.24; 'certain': 0.27; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'received:172.16': 0.29; 'am,': 0.29; 'lines': 0.31; 'file:': 0.31; 'sep': 0.31; 'file': 0.32; 'this.': 0.32; 'another': 0.32; 'raw': 0.33; 'skip:# 10': 0.33; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'false': 0.36; 'words,': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'hi,': 0.36; 'should': 0.36; 'so,': 0.37; 'two': 0.37; 'starting': 0.37; 'skip:o 20': 0.38; 'message-id:@gmail.com': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'delete': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'till': 0.61; "you're": 0.61; 'back': 0.62; "you've": 0.63; 'header:Message-Id:1': 0.63; 'more': 0.64; 'different': 0.65; 'details': 0.65; 'line,': 0.68; 'repeat': 0.74; 'different.': 0.84; 'off,': 0.84; 'subject:over': 0.84; '2013,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to; bh=ULGlo1TGz+qJJRoyJWuMkbh9ReEmTA7Y3ZEkgdqy3LM=; b=gmxh0o29exk8WlWQl/wfIgsQ+vP01153NNlqMhRYG31XNfx4aGOwyeek4hjaseRB2V WIlN0GOZirh7LrYuVzmAwquMM6fUORVJj/GtCqFL4IZ+icZFCTSoomhxD4Lzle1JCUu1 s2+5okG5KmMZIA9qRqP4AzACbrrp6i6S1PRl35sxy5C+qrDLab/Sx/9Dx0/2UeZyClFf nFIgrDuz/gPVlAFH7gtP4ySg8I+sC9at0LLLXUtmAoNsdLldGZ6/zj3Rud6tQtrBpFdb UGPnnghYzaZuTeWV/E9GwfpJwHTDzj3guhqXv0RtP1pU+kpMqj5alfLheq875JS+ZFNL BCQQ== X-Received: by 10.66.136.227 with SMTP id qd3mr20271138pab.113.1379521136205; Wed, 18 Sep 2013 09:18:56 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) Subject: Re: iterating over a file with two pointers From: Travis Griggs In-Reply-To: Date: Wed, 18 Sep 2013 09:18:54 -0700 Content-Transfer-Encoding: quoted-printable References: <3018b3d4-f914-4c89-9f26-cd4b2af32e73@googlegroups.com> To: "python-list@python.org" X-Mailer: Apple Mail (2.1508) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379521144 news.xs4all.nl 15969 [2001:888:2000:d::a6]:58965 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54388 On Sep 18, 2013, at 5:07 AM, nikhil Pandey = 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 = wrote: >>=20 >>> hi, >>=20 >>> 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. >>=20 >>> 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? >>=20 >>> please help. I am stuck up on this. >>=20 >>=20 >>=20 >> After the inner loop finishes, do you want to go back to where the >>=20 >> outer loop left off, or should the outer loop continue from the point >>=20 >> where the inner loop stopped? In other words, do you want to locate >>=20 >> overlapping sections, or not? Both are possible, but the solutions >>=20 >> will look somewhat different. >>=20 >>=20 >>=20 >> ChrisA >=20 > 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 =3D False for line in (raw.strip() for raw in file): if reversing: print('____', line[::-1], '____') reversing =3D not line.endswith('+') else: print(line) reversing =3D line.startswith('*') Which begins reversing lines as its working through them, until a = different condition is met. Travis Griggs =09=