[BangPypers] [Novice] Question on File seek and tell methods
davidsnt
davidsnt at gmail.com
Wed Jun 12 21:48:06 CEST 2013
Group,
I need a little help in using the file seek and tell methods, I have a file
with n lines, in which I have to find out the line which starts from a
particular keyword and print all the lines until I find next matching
keyword.
I did a work around using seek and tell, I used regex as well. I opened the
file, read line by line in a for loop, and checked each line for the
keyword used tell() to get the position and the seek() to move my cursor
and tried to print next lines until I get the next matching keyword but I
failed can you help me here.
Example test file
test.log
Bangalore
Hyderabad
Chennai
Mumbai
Tennis
Cricket
Poker
Angry Birds
Cricket
Mumbai
Chennai
I want to read and print from Chennai to Angry Birds
import sys,re
f = open('test.log', 'r')
for line in f:
match = re.search('^Chennai', line)
if(match):
f.seek(f.tell(), 0)
<I am missing out the logic from here>
Thanks,
David
More information about the BangPypers
mailing list