[Tutor] File IO help

Mike Haft m.haft at abdn.ac.uk
Thu Oct 27 02:01:22 CEST 2005


Hello all,
        I'm new to python but so far I have to say its a really good language

I've been having some trouble with File IO can anyone help? I've got the
basics but my problem is that I have many files (one for each year of the
last 100 years or so) that look like this:

MONTH  RAIN   AVTEMP  RAD  EVAP
****************************************
1      12.4    12.0    *   10
2      13.9    30.0    *   11
3

etc until month 12

So far all I know how to do in Python looks something like this:

def readInFile(inputName):
    input = open(inputName, "r")
    result = []
    for line in input:
        if line[:1] == "1":
            fields = line.split()
            data = fields[1] + fields[2] + fields[7]
            result.append(data)
    input.close()
    return result

Thats basically all I've been able to do but I need to write script that
opens a file, reads the relevent data, stores that data and then closes
the file and moves on to the next file repeating the action until all the
files have been read. Then it needs to write a file with all the data it
has collected but in a different format.

If anyone can help I'd be really grateful. I hope I'm not asking too much
of the list but I haven't found anything that says READ THIS BEFORE
POSTING! on it to tell me otherwise.

Thanks in advance

Mike Haft



More information about the Tutor mailing list