Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'binary': 0.07; 'lines,': 0.07; 'subject:file': 0.07; 'subject:two': 0.07; 'assuming': 0.09; 'iterate': 0.09; 'performs': 0.09; 'pointers': 0.09; 'windows,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; '(say': 0.16; '20)': 0.16; 'caching': 0.16; 'cc:name:python list': 0.16; 'circumvent': 0.16; 'file))': 0.16; 'loop.': 0.16; 'objects.': 0.16; 'roy': 0.16; 'unix-style': 0.16; 'unwound': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'basically': 0.19; 'file,': 0.19; 'addition,': 0.20; 'help.': 0.21; 'seems': 0.21; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'holds': 0.26; 'certain': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'mode': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'lines': 0.31; 'another.': 0.31; 'sep': 0.31; 'work:': 0.31; 'yourself.': 0.31; 'file': 0.32; 'this.': 0.32; 'another': 0.32; 'text': 0.33; 'open': 0.33; 'url:python': 0.33; '(i.e.': 0.33; "i'd": 0.34; 'could': 0.34; 'subject:with': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'next': 0.36; 'possible': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'two': 0.37; 'starting': 0.37; 'minimum': 0.38; 'system,': 0.38; 'url:library': 0.38; 'files': 0.38; 'issue': 0.38; 'pm,': 0.38; 'expect': 0.39; 'how': 0.40; 'read': 0.60; 'dave': 0.60; 'hope': 0.61; 'till': 0.61; 'strictly': 0.61; "you're": 0.61; 'more': 0.64; 'smith': 0.68; 'legal': 0.71; 'article': 0.77; 'oscar': 0.84; 'subject:over': 0.84; 'angel': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=3efIiGk43VVrLwK1lMX+kV82muVMGAIlwAOAN7j5SAw=; b=tPZnVZAIjvZ4WuCCcKTf6S6ZOfXvCAA0Fux5762JLfQjhtr/vhWgtwrZCKzSodfjxA FtJtLvabrSZ5Cw///dMGhQVzHfBdMbz8UiYvAMHUEix6dVmrHdHp++CCErcnGZyH+6q1 NvfKK6S/SHb8NsbiHFiNbD2qhq/xlg6CNp3bQ8YQeGR3B6MFpX6joG4RTIDtUvlciPrg F7QkgBii6KJP5y27aA9YvYZnAMky2UimH/5SY9croy9SArQWN5GNDTuFxTjLBKu57VwW fp78b2SrDqBXxK3YRX/rG9T+3113WMCC/M+xRNj1xdtKojogNuANrpVYDzUidPepR0B4 ymIA== X-Received: by 10.220.74.69 with SMTP id t5mr17405856vcj.18.1379509777975; Wed, 18 Sep 2013 06:09:37 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <3018b3d4-f914-4c89-9f26-cd4b2af32e73@googlegroups.com> From: Oscar Benjamin Date: Wed, 18 Sep 2013 14:09:17 +0100 Subject: Re: iterating over a file with two pointers To: Roy Smith Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379509785 news.xs4all.nl 15870 [2001:888:2000:d::a6]:55162 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54380 On 18 September 2013 13:56, Roy Smith wrote: > >> > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey >> > 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. >> [...] > > In article , > Dave Angel wrote: > [I hope I unwound the multi-layer quoting right] >> In addition, is this really a text file? For binary files, you could >> use seek(), and manage things yourself. But that's not strictly legal >> in a text file, and may work on one system, not on another. > > Why is seek() not legal on a text file? The only issue I'm aware of is > the note at http://docs.python.org/2/library/stdtypes.html, which says: > > "On Windows, tell() can return illegal values (after an fgets()) when > reading files with Unix-style line-endings. Use binary mode ('rb') to > circumvent this problem." > > so, don't do that (i.e. read unix-line-terminated files on windows). > But assuming you're not in that situation, it seems like something like > this this should work: > >> I'd suggest you open the file twice, and get two file objects. Then you >> can iterate over them independently. There's no need to use OS resources by opening the file twice or to screw up the IO caching with seek(). Peter's version holds just as many lines as is necessary in an internal Python buffer and performs the minimum possible amount of IO. I would expect this to be more efficient as well as less error-prone on Windows. Oscar