[Tutor] How to print the next line in python

ranjan das ranjand2005 at gmail.com
Sat Sep 12 11:34:39 CEST 2009


Thanks a lot Andre...it is working

On Sat, Sep 12, 2009 at 2:17 PM, Andre Engels <andreengels at gmail.com> wrote:

> On Sat, Sep 12, 2009 at 10:35 AM, ranjan das <ranjand2005 at gmail.com>
> wrote:
> > Hi,
> >
> > I am new to python and i wrote this piece of code which is ofcourse not
> > serving my purpose:
> >
> > Aim of the code:
> >
> > To read a file and look for lines which contain the string 'CL'. When
> found,
> > print the entry of the next line (positioned directly below the string
> 'CL')
> > ....continue to do this till the end of the file (since there are more
> than
> > one occurrences of 'CL' in the file)
> >
> > My piece of code (which just prints lines which contain the string 'CL')
> >
> > f=open('somefile.txt','r')
> >
> > for line in f.readlines():
> >
> >      if 'CL' in line:
> >               print line
> >
> >
> > please suggest how do i print the entry right below the string 'CL'
>
> I would this using a boolean variable to denote whether the line
> should be printed:
>
> printline = false
> for line in f.readlines():
>     if printline:
>        print line
>    printline = 'CL' in line
>
> (I assume the last line does not contain 'CL', because otherwise we
> have a problem with the problem definition)
>
> --
> André Engels, andreengels at gmail.com
>



-- 
The inherent vice of capitalism is the unequal sharing of blessings; the
inherent virtue of socialism is the equal sharing of miseries.

~ Winston Churchill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090912/c9d88994/attachment.htm>


More information about the Tutor mailing list