problems iterating over a files lines

Jesse Noller jnoller at reference-info.com
Wed Jan 21 11:00:39 EST 2004


I am a relative newbie to python and I am having issues trying to
iterate over the lines of a file.

I have a text file - foo.bar inside of this file are lines of text:

x-3411342
y-1324123
w-2314121

Each with a trailing \n to designate the end of the line.

I am trying to read this file into python - which is simply in and of
itself, however:

file = open("foo.bar", "rb")
while 1:
    lines = file.open().split('\n'):
    if not lines:
           break
    for lines in lines:
           key = line
           get.key(key)
           results = [key, get.up, get.down]
file.close()

Appends an extra "blank" line to the list of lines, which causes the
program to crash - secondly, I've looked at about 20 examples, and all
of them (including this one) suffer from the problem of looping over
the lines for the number of lines.

What I want to do is open the contents of the file - read the line and
set a variable - key, and for each line in the file, I want to do
something with that key - but only once. So I want to read in a line,
set to a variable, do something, print, and then do it for the next
line.


Any help is appreciated. Thank you.



More information about the Python-list mailing list