[Tutor] How to skip to next line in for loop
bob gailer
bgailer at gmail.com
Fri May 2 01:08:37 CEST 2008
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 approach, then each line will be a digit followed
by \n (newline). So no line will == "3". Instead use:
if line[:-1] == "3":
> line.next
Since next is a method it must be called, so that becomes:
line.next()
The rest is fine.
> 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?
--
Bob Gailer
919-636-4239 Chapel Hill, NC
More information about the Tutor
mailing list