[Tutor] File IO Help again

Mike Haft m.haft at abdn.ac.uk
Thu Oct 27 21:18:42 CEST 2005


Apologies for not making things clearer last time.

My specific problems are:

why can I not get the readline() or readlines() functions to work, I'm
told they are not defined whenever I try.

Also the following:

if line[:1] == "1":

collects data from month 1, 10, 11, and 12. How do I make the readfile
function I defined count through 1 to 12 and retreive the fields I need?
Can I just use a for loop type of construction and substitute an x
variable where there is curently a [:1] and a "x" i.e.:

if line[:y] == x:

If not why not? What must I do.

Other questions coming soon. Sorry for the vague post earlier,
multitasking isn't my specialty, especially without coffee.

Thanks

Mike

>From the previous post:
-----------------------

The data in each file looks liek:

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



More information about the Tutor mailing list