Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54380

Re: iterating over a file with two pointers

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 <oscar.j.benjamin@gmail.com>
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 <roy-B13238.08561818092013@news.panix.com>
References <3018b3d4-f914-4c89-9f26-cd4b2af32e73@googlegroups.com> <CAPTjJmoyrJqVR29MeDzcfA9K=gGgHSuqO3uCNXGLQs7APLJByA@mail.gmail.com> <mailman.115.1379504419.18130.python-list@python.org> <roy-B13238.08561818092013@news.panix.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Wed, 18 Sep 2013 14:09:17 +0100
Subject Re: iterating over a file with two pointers
To Roy Smith <roy@panix.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.119.1379509785.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 18 September 2013 13:56, Roy Smith <roy@panix.com> wrote:
>
>> > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey <nikhilpandey90@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.
>> [...]
>
> In article <mailman.115.1379504419.18130.python-list@python.org>,
>  Dave Angel <davea@davea.name> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

iterating over a file with two pointers nikhil Pandey <nikhilpandey90@gmail.com> - 2013-09-18 04:12 -0700
  Re: iterating over a file with two pointers Chris Angelico <rosuav@gmail.com> - 2013-09-18 21:21 +1000
    Re: iterating over a file with two pointers nikhil Pandey <nikhilpandey90@gmail.com> - 2013-09-18 05:07 -0700
      Re: iterating over a file with two pointers Travis Griggs <travisgriggs@gmail.com> - 2013-09-18 09:18 -0700
  Re: iterating over a file with two pointers Dave Angel <davea@davea.name> - 2013-09-18 11:39 +0000
    Re: iterating over a file with two pointers Roy Smith <roy@panix.com> - 2013-09-18 08:56 -0400
      Re: iterating over a file with two pointers Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-18 14:09 +0100
      Re: iterating over a file with two pointers Roy Smith <roy@panix.com> - 2013-09-18 10:36 -0400
      Re: iterating over a file with two pointers Dave Angel <davea@davea.name> - 2013-09-18 20:07 +0000
      Re: iterating over a file with two pointers Peter Otten <__peter__@web.de> - 2013-09-19 09:23 +0200
      Re: iterating over a file with two pointers Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-19 15:16 +0100
      Re: iterating over a file with two pointers Peter Otten <__peter__@web.de> - 2013-09-19 16:38 +0200
      Re: iterating over a file with two pointers Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-19 15:48 +0100
  Re: iterating over a file with two pointers Peter Otten <__peter__@web.de> - 2013-09-18 13:44 +0200
    Re: iterating over a file with two pointers nikhil Pandey <nikhilpandey90@gmail.com> - 2013-09-18 05:14 -0700
      Re: iterating over a file with two pointers Peter Otten <__peter__@web.de> - 2013-09-18 14:54 +0200
      Re: iterating over a file with two pointers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-19 02:40 +0000
  Re: iterating over a file with two pointers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-19 02:56 +0000
    Re: iterating over a file with two pointers Joshua Landau <joshua@landau.ws> - 2013-09-19 08:04 +0100

csiph-web