[Tutor] How to skip to next line in for loop

Brain Stormer brnstrmrs at gmail.com
Fri May 2 16:05:37 CEST 2008


Well,
I was somewhat confused with all of the answers so I decided to go with
my/following method.  Kent's method has 4 fewer lines of code than mine and
cleaner.  Please correct me if I am fundamentally wrong.

f=open('file.txt',r)

for line in f.read().split():
    if line == "3"
          position = True
    else:
          position = False
    if position == True
          print line
          position = False
f.close()

On Fri, May 2, 2008 at 6:55 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Fri, May 2, 2008 at 3:55 AM, Roel Schroeven
> <rschroev_nospam_ml at fastmail.fm> wrote:
>
> >  Shouldn't it even be 'line = f.next()'?
>
> Wow, I think Brain Stormer should get a prize. I'm not sure what the
> prize is, but his short program has elicited incomplete and inaccurate
> answers from three of the top posters to this list! I *think* this is
> a complete answer:
>
> f = open('file.txt',r)
> for line in f:
>     if line.rstrip() == "3":
>           line = f.next()
>           print line
>           break # put this in if there is only one line you want to print
> f.close()
>
> ...awaiting any further corrections :-)
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080502/671214e1/attachment.htm>


More information about the Tutor mailing list