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

2008-05-02 Thread bob gailer
bob gailer wrote: Or even simplre f = open('file.txt',r).readlines() print [f[x+1] for x, line in enumerate(f) if line.rstrip() == "3"][0] -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/ma

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

2008-05-02 Thread Kent Johnson
On Fri, May 2, 2008 at 11:42 AM, Paul McGuire <[EMAIL PROTECTED]> wrote: > f=open('file.txt',r) > print_line = False > for line in f: >if print_line: > print line > print_line = False >if line == "3": Don't forget about the newline...(that makes four!) Kent _

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

2008-05-02 Thread Paul McGuire
Augghh! I can't stand it!!! If position is a boolean, then *why* must we test if it is equal to True?!!! It's a boolean! Just test it! For that matter, let's rename "position" to something a little more direct, "print_line" perhaps? Did you know that files are now iterators? If going through

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

2008-05-02 Thread Kent Johnson
On Fri, May 2, 2008 at 10:34 AM, Brain Stormer <[EMAIL PROTECTED]> wrote: > f=open('file.txt',r) > position =False > > for line in f.read().split(): Note that split() splits on any whitespace, not just line endings. In your case it doesn't much matter I guess. Kent _

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

2008-05-02 Thread Brain Stormer
f=open('file.txt',r) position =False for line in f.read().split(): if position == True print line position = False if line == "3": position = True else: position = False f.close() On Fri, May 2, 2008 at 10:28 AM, Brain Stormer <[EMAIL PROTECTED]> wrot

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

2008-05-02 Thread Brain Stormer
You are correct. It is missing the ":" and it will print "3" On Fri, May 2, 2008 at 10:18 AM, bob gailer <[EMAIL PROTECTED]> wrote: > Brain Stormer wrote: > > > 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

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

2008-05-02 Thread bob gailer
Brain Stormer wrote: 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 l

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

2008-05-02 Thread Brain Stormer
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" positi

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

2008-05-02 Thread Kent Johnson
On Fri, May 2, 2008 at 3:55 AM, Roel Schroeven <[EMAIL PROTECTED]> 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

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

2008-05-02 Thread Roel Schroeven
Alan Gauld schreef: if line == "3": line.next this then becomes f.next() # next is a method not an attribute so needs the () to call it Shouldn't it even be 'line = f.next()'? -- The saddest aspect of life right now is that science gathers knowledge faster than society gathe

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

2008-05-01 Thread Alan Gauld
"bob gailer" <[EMAIL PROTECTED]> wrote if line == "3": Assuming you adopt my approach, then each line will be a digit followed by \n (newline). So no line will == "3". Instead use: if line[:-1] == "3": or if line.rstrip() == "3" or even if line.startswith("3"): personall

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

2008-05-01 Thread Alan Gauld
"Brain Stormer" <[EMAIL PROTECTED]> wrote f = open('file.txt',r) for line in f.read(): This reads the whole file inta a string then iterates over all the characters(not lines) in that string. Better to iterate over the file: for line in f: if line == "3": line.next thi

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

2008-05-01 Thread bob gailer
Brain Stormer wrote: I have the following code: f = open('file.txt',r) for line in f.read(): That will read the entire file into a string, then access one character at a time from the string. If you want to work with lines: for line in f: if line == "3": Assuming you adopt my approac

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

2008-05-01 Thread Kent Johnson
On Thu, May 1, 2008 at 5:04 PM, Brain Stormer <[EMAIL PROTECTED]> wrote: > I have the following code: > > f = open('file.txt',r) > for line in f.read(): > if line == "3": >line.next Try f.next() line is a string, it doesn't have a next() method. The file itself is iterable and

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

2008-05-01 Thread Brain Stormer
I have the following code: f = open('file.txt',r) for line in f.read(): if line == "3": line.next print line f.close() The file.txt looks like 1 2 3 4 5 I would like the code to output "4" but I don't know how to use the next. Are there any other way? Thanks ___