473,372 Members | 1,240 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 2051
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: ^ | last post by:
Hi group, I've become interested in Python a while ago and just converted a simple perl script to python. The script is very simple, it generates a list of found virusses from some maillog files...
5
by: Richard | last post by:
Hi, Can anyone tell me what the difference is between for line in file.readlines( ): and for line in file:
20
by: Lucas Raab | last post by:
I'm done porting the C code, but now when running the script I continually run into problems with lists. I tried appending and extending the lists, but with no avail. Any help is much appreciated...
3
by: Wade G. Pemberton | last post by:
Help!: I use javascript to format repetitive data from a web page input FORM into a long string , and save it as lines of comma delimited data in a text file on a Unix server. The text...
7
by: kamkwokho | last post by:
Could any answer following questions as many as you can. ii) Write a C shell script convertmin which will read in a number, thought of as representing minutes, and print out the number of...
1
by: llauzon | last post by:
I'm having trouble getting PHP installed with Apache. I don't know if it's tied to my trying to use the newest versions available on the respective application web sites. I've successfully...
12
by: Bruce One | last post by:
For all the ORM i have searched around, I have always found two big problems: 1) To update or delete a set of records you must first bring it to memory. If you are inside a loop and have to do it...
6
by: Eric_Dexter | last post by:
I am writing out zero byte files with this (using python 2.5). I have no idea why I am having that problem, I am also looking for an example of readlines where I can choose a number of lines say...
1
by: Avi1 | last post by:
Hi, I got the code (from the internet)for comparing two files and showing the difference in contents.Now,I tried the same code for two files written in japanese language(kanji).If I save the two...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.