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


Groups > comp.lang.python > #54379

Re: iterating over a file with two pointers

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: iterating over a file with two pointers
Date 2013-09-18 08:56 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-B13238.08561818092013@news.panix.com> (permalink)
References <3018b3d4-f914-4c89-9f26-cd4b2af32e73@googlegroups.com> <CAPTjJmoyrJqVR29MeDzcfA9K=gGgHSuqO3uCNXGLQs7APLJByA@mail.gmail.com> <mailman.115.1379504419.18130.python-list@python.org>

Show all headers | View raw


> > 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.

and use tell() to keep them in sync.  Something along the lines of (not 
tested):

f1 = open("my_file")
f2 = open("my_file")

while True:
   where = f1.tell()
   line = f1.readline()
   if not line:
      break
   if matches_pattern(line):
      f2.seek(where)
      for i in range(20):
         line = f2.readline()
         print line

Except for the specific case noted above (i.e. reading a unix file on a 
windows box, so don't do that), it doesn't matter that seek() does funny 
things with windows line endings, because tell() does the same funny 
things.  Doing f2.seek(f1.tell()) will get the two file pointers into 
the same place in both files.

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