iterating over a file with two pointers
Roy Smith
roy at panix.com
Wed Sep 18 08:56:18 EDT 2013
> > On Wed, Sep 18, 2013 at 9:12 PM, nikhil Pandey <nikhilpandey90 at 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 at python.org>,
Dave Angel <davea at 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.
More information about the Python-list
mailing list