Iteration through a text file.

DavidW Blaschke dwblas at
Thu Jun 5 22:02:13 CEST 2003

You want to write it in a way that you will
understand.  If you are a newbie then you want to keep
it simple.  (Nice word newbie, no negative connations,
but instead is someone trying to improve).  Anyway, I
would suggest that you spilt the data up into
understandable portions.

The following is some code that I think does what you
want.  It stores all the lines for one server in a
list.  That list is then processed by a function and
the detail is printed.  Then, the next server is
processed, etc.  If you feel more comfortable writing
the data for one server to a file and then processing
it, do that by all means.  Do it in a way that you
understand.  This could all be done with nested loops,
but I donlt think that would work for you.  BTW, if at
any time I sound like I'm talking down to you please
forgive me.  It is just the way my own simple mind
works.   Note the ErrorPrint function.  Put that
somewhere so you can import it anytime you use
try/except.  It took a while for me to find out how to
do this, so I hope it will save you some grief.  If
you save this message as a text file, you can delete
all of these first comments and run the program as is
on a linux system.  It will give you an error from
ErrorPrint() because it can't find the filename that's
on the final line.  Then change the filename to
whatever yours is and run it again.



##  Program Name:

import sys
import string

def PrintMain( filename ) :
    ##-----  Read the file
    try :
        fp = open( filename, "r" )
        data = fp.readlines()

        server_list = []
        server_ctr = 0
        server_name = ""
        for eachRec in data :
            ##---  Eliminate any upper/lower case
            new_rec = string.upper( eachRec )
            ##---  Replace newline and end of rec
            new_rec = string.replace( new_rec, "\n", "
" )
            new_rec = string.strip( new_rec )

            ##--- Note that we only want to find
"server" if it is the
            ##    first word, not if it's burried in
the text
            found_server = cmp( new_rec[:7], "SERVER,"
            if 0 == found_server :

                ##--- don't process if this is the
first server found
                if server_ctr :
                    Process_Server( server_name,
server_list )

                server_ctr += 1
                substrs = string.split( new_rec )
                server_name = substrs[1]
                server_list = []           ## blank
the list
            else :
                server_list.append( new_rec )

        ##---  Process the final batch of recs
        Process_Server( server_name, server_list )
    except :
        print "Could NOT open filename %s" %

## END PrintMain()

##   Process the recs associated with a server
def Process_Server( server_name, rec_list ) :
    ##---  Now, look for system availability
    ##---  I like to use a field with the look-for
sting in it,
    ##     that way it can be easily changed 
    look_for = "SYSTEM AVAILABILITY"
    len_lf = len(look_for)
    found = 0

    print "\n----> for server %s" % (server_name)
    for eachRec in rec_list :
        found_server = cmp( eachRec[:len_lf], look_for
        if 0 == found_server:
            found = 1
            ##--- just print it - you can massage it
any way you like
            substrs = string.split( eachRec )
            len_subs = len(substrs)

            ##--- don't print substrs[0] = "Server" or

            ##    substrs[1] = "Availability"
            for j in range( 2, len_subs ) :
                print "       %s" % (substrs[j])

    ##---  No "System Availability" found
    if not found :
        print "       N/A - None\n"

## END Process_Server()

##   print details of try / except errors
def ErrorPrint( ) :
    import Tkinter
    import traceback
    et, ev, tb = sys.exc_info()
    while tb :
        co = tb.tb_frame.f_code
        filename = "Filename = " + str(co.co_filename)
        line_no =  "Error Line # = " +
        print filename
        print line_no
        tb = tb.tb_next
    print "et = ",
    print et
    print "ev = ",
    print ev

## END ErrorPrint()

if __name__ == "__main__" :
    filename = "print_stuff.txt"
    PrintMain( filename )

Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.

More information about the Python-list mailing list