469,304 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

problems iterating over a files lines

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.
Jul 18 '05 #1
2 1904
On Wednesday 21 January 2004 4:00 pm, Jesse Noller wrote:
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.
First I take it:
lines = file.open().split('\n'):
should have been:
lines = file.read().split('\n'):


If you used file.read().splitlines() instead you would avoid the extra blank
line.

A much better way to iterate line-by-line through the list is simply:

for line in file("foo.bar", "rb"):
# do something to line

(I'm using the built-in file() function - strictly type - which is the new
name for open().)

I don't know where your "get" comes from so I can't help you with the rest.

James
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #2
On Wednesday 21 January 2004 4:27 pm, James Henderson wrote:
If you used file.read().splitlines() instead you would avoid the extra
blank line.


Or even file.readlines(). D'oh!

Though this keeps the newlines on the end of each file.
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by ^ | last post: by
20 posts views Thread by Lucas Raab | last post: by
3 posts views Thread by Wade G. Pemberton | last post: by
7 posts views Thread by kamkwokho | last post: by
6 posts views Thread by Eric_Dexter | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.