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().spl it('\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 2070
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().spl it('\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().spl it('\n'):
should have been:
lines = file.read().spl it('\n'):
If you used file.read().spl itlines() 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 for further processing.
I've found that there's a huge difference in execution time for the scripts,
in favor of perl and I can't pinpoint what's going wrong;
perl runs:
0.07 real 0.05 user 0.01 sys
0.07 real 0.05 user ...
|
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
Please see both the Python and C code at
http://home.earthlink.net/~lvraab. The two files are ENIGMA.C and engima.py
TIA
|
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 file is meant to be downloaded
to MS Excel on the users' computers. Several such files will be
downloaded and appended into the Excel file. Problem: The text file
looks good when viewed offline by any text reader, but Excel will not
import it...
|
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 hours/minutes it
represents so:
$ convertmin
Enter a number of minutes:
128
| |
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 installed Apache and when browsing http://localhost/ I can see the files I've put there. I've even re-directed from the DocumentRoot folder from "htdocs" to a folder on the root of my C:\ drive.
After installing PHP, I've updated the httpd.con file to...
|
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 n times, then you have to query
this set n times as well. Would be nice if ORM could make it easier (in a
strongly type manner) to call a dynamic query to directly update/delete
records.
2) I have noticed all ORM work with collection as thei...
|
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 lines 12 to 14
and then write them back to disk. any help would be apreaceted.
import sys as sys2
import os as os2
|
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 japanese .txt files in ANSI format,it works fine,but, if I save them in formats like 'UTF-8','unicode','unicode bigendian',it doesn't show the differences properly....keeps showing odd symbols instead of the japanese characters.
Would be glad if...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |