Iteration through a text file.
mvs2 at linuxmail.org
Wed Jun 4 20:01:05 CEST 2003
I'm pretty sure I'm missing something very basic, but don't know where
I have a text file that a Sys Admin is using a program to generate
(don't know the program). It contains a list of Servers, along with
their "System Availablity" percentages. Some Servers the program
cannot reach, and therefore it has errors in the text file instead of
I am trying to loop through the text file, looking for the word
"Server." If I find that word, I then print out the server name (will
write out later to another file, but for now printing shows me where
Then, I want to look for the phrase "System Availablilty." If I find
that phrase, then I'll print out the percentage.
If I hit the next occurrence of the word "Server," , and have not had
an occurrence of "System Availability," then I'll know System
Availability was not found.
The trick for me is how do I start searching again for System
Availability after printing out the next server's name?
Here's the code:
filename = r'C:\TextTest\combo.txt'
file = open (filename, 'r')
it = iter(file)
for line in it:
if line.find("Server") >= 0:
stripped = line.strip()
sname = stripped.split(',')
print sname[1:] #this is the server name.
for line in it: #look for availability or next server name
#whichever comes first
if line.find("System Availability") >= 0: #found it
stripped2 = line.strip()
avail = stripped2.split(':')
print avail[1:] #availability percentage
elif line.find("Server") >= 0: #have found next server, so
I know the code is flawed, and it appears that line just keeps
incrementing from where it left off (which I thought was good), and I
think that makes sense to me.
What I want to know is how do I point line back to the area it started
the inner loop with?
After Seeing the first "Server" and finding no availability, how do I
go to the next "Server?"
I hope this kind of makes sense.
More information about the Python-list