[BangPypers] BangPypers Digest, Vol 70, Issue 17

Saurabh Hirani saurabh.hirani at gmail.com
Mon Jun 17 12:57:34 CEST 2013


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/



On Mon, Jun 17, 2013 at 3:30 PM, <bangpypers-request at python.org> wrote:

> Send BangPypers mailing list submissions to
>         bangpypers at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://mail.python.org/mailman/listinfo/bangpypers
> or, via email, send a message with subject or body 'help' to
>         bangpypers-request at python.org
>
> You can reach the person managing the list at
>         bangpypers-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of BangPypers digest..."
>
>
> Today's Topics:
>
>    1. Re: [Novice] Question on File seek and tell methods (Vishal)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 17 Jun 2013 01:22:23 +0530
> From: Vishal <vsapre80 at gmail.com>
> To: Bangalore Python Users Group - India <bangpypers at python.org>
> Subject: Re: [BangPypers] [Novice] Question on File seek and tell
>         methods
> Message-ID:
>         <CACPguY-jfVNb85_==Mvih0-Chj1Ws-rA=-
> dRGbyYKGjwp1JE3g at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> Hi,
>
> If the file you are talking about is not too big...i mean has less than say
> 50,000 lines, then the simplest would be to do:
>
> with open('fileName') as fin:
>     lines = fin.readlines()
>
> printStuff = False
> for line in lines:
>     if 'start_keyword' in line:
>         printStuff = True
>         print line
>
>     elif 'stop_keyword' in line:
>         print line
>         break
>
>     elif printStuff:
>         print line
>
> Hope this helps,
> Vishal Sapre
>
>
>
> Thanks and best regards,
> Vishal Sapre
>
> ---
> "Life is 10% how you make it, and 90% how you take it"
> "????? ?????, ????? ????? (Benefit for most people, Happiness for most
> people.)"
> ---
> Please DONT print this email, unless you really need to. Save Energy &
> Paper. Save the Earth.
>
>
> On Thu, Jun 13, 2013 at 1:18 AM, davidsnt <davidsnt at gmail.com> wrote:
>
> > 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
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>
> ------------------------------
>
> End of BangPypers Digest, Vol 70, Issue 17
> ******************************************
>



-- 
Saurabh


More information about the BangPypers mailing list