That's like saying, "Head north until you get to Hyderabad. Once you're
in Hyderabad, write down your GPS coordinates. Go to those GPS
coordinates. Keep going north, taking pictures along the way, until you
get to Nagpur. Once in Nagpur, write down your GPS coordinates. Go to
those GPS coordinates. Stop taking pictures."
Alternately, you could read through the file once, making a note of the
locations at which 'Chennai' and 'Angry Birds' occur, then later jump to
those points and print them:
starts = []
stops = []
matched_strings = []
start_str = 'Chennai'
stop_str = 'Angry Birds'
with open(inputfile) as f:
for line in f:
if start_str in line:
# don't record extra starts in between starts and stops
if len(starts) == len(stops):
# our file cursor is now after our target, so let's adjust
start = f.tell() - len(line)
starts.append(start)
elif stop_str in line:
# don't record stops that aren't after a start
if len(starts) == len(stops) + 1:
stops.append(f.tell() - len(line))
# make sure we have a stop for every start
if len(starts) > len(stops):
stops.append(f.tell()) # this is the file end
for start, stop in zip(starts, stops):
f.seek(start)
matched_strings.append(f.read(stop - start))
On 6/17/2013 5:49 PM, davidsnt wrote:
Thank you all for your help, I managed to get what I wanted from the file
with all your help, but still I have my question left opened, cant we get
this done with seek and tell methods, agree that we have a better way, but
want to know the possibilities of getting done with seek and tell.
Read the file, check for matching pattern, get the cursor position with
tell, seek the cursor to the same position and start reading from there
until the next matching position, can you help me with seek and tell
methods to achieve the above case.
Thanks,
David
On Mon, Jun 17, 2013 at 4:46 PM, saurabh <saurabh.hir...@gmail.com> wrote:
Jonathan has given quite a good answer which I think would solve your
problem. Here's my take on it. The way of looking at it is a little
different but Jonathan's approach is much simpler.
The problem is that when you do "for line in f" you are using Python's
iterators and they are not rewindable i.e. "for line in f" gives call the
next() function but there is no way to go back when you use iterators. You
can solve your problem with the following snippet of code.
#!/usr/bin/env python
import re
import sys
# not doing input validation
# call program as: python prog.py start_keyword stop_keyword file_to_read
startat = sys.argv[1]
stopat = sys.argv[2]
inputfile = sys.argv[3]
re_startat = re.compile(r'^%s$' % (startat))
re_stopat = re.compile(r'^%s$' % (stopat))
pattern = re_startat
with open(inputfile) as f:
line = f.readline()
inrange = False
while line:
match = re.search(pattern, line)
# if the pattern matches
if (match):
# and we are not in range of startat - stopat
if (not inrange):
# get in range and change the pattern
inrange = True
pattern = re_stopat
else:
# we are in the range => stopat pattern matched
# print line and exit
print line
break
if (inrange):
# we are in range + stopat pattern not matched
# keep on printing
print line
line = f.readline()
It does look prettier in vim on a black screen though :)
The idea is you are "in range" if you have matched your first keyword and
till you stay in the range you keep on printing. When you find your next
keyword, you are "out of the range" and you exit.
Hope that helps.
--
regards,
Saurabh.
http://curiosityhealsthecat.blogspot.in/
--
View this message in context:
http://python.6.x6.nabble.com/Novice-Question-on-File-seek-and-tell-methods-tp5021174p5021552.html
Sent from the Bangalore (BangPypers) mailing list archive at Nabble.com.
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers