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. 2 2052
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |