[Tutor] File IO

Liam Clarke-Hutchinson Liam.Clarke-Hutchinson at business.govt.nz
Thu Nov 3 23:11:36 CET 2005


Hi Mike, 

Same file, eh?



>From the docs for split()

"If sep is not specified or is None, a different splitting algorithm is
applied. First, whitespace characters (spaces, tabs, newlines, returns, and
formfeeds) are stripped from both ends. Then, words are separated by
arbitrary length strings of whitespace characters. Consecutive whitespace
delimiters are treated as a single delimiter ("'1 2 3'.split()" returns
"['1', '2', '3']"). Splitting an empty string or a string consisting of just
whitespace returns an empty list."

So, these lines 

'1	22.5	0.3	*	54.6	15.1	*	11.9'

will yield  lists - 

['MONTH','RAIN', 'AVTEMP','S10','RAD','SUN','WIND','EVAPW']
['1','22.5','0.3','*','54.6','15.1','*','11.9']

which is what you're after. 

>data = fields[1] + fields[2] + fields[7]
>IndexError: list index out of range

Now, this error. 

>>> x = [1,2,3]
>>> print x[5] 

will cause the same error. 

Here's a debugging suggestion, jump in the interactive interpreter, and try
this -

>>>fields = "Monthly Weather Data, LAU73M.MET, converted from:".split()
>>>print fields
["Monthly","Weather","Data","LAU73M.MET","converted", "from:"]
>>>print fields[7]
IndexError: list index out of range
>>>"**************************************************************".split()
['**************************************************************']

That's your problem.


In your loop, I'd recommend you check and ignore your header.

either - 

result = []
pastHeader = False
    for line in input:
        if not pastHeader:
            if line.startswith("***********"):
                pastHeader = True #Will be shortly, anyway.
            continue #Skip line, go to next.
        else:
            #Header has passed. Process line normally.
            fields = line.split()



-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Michael Haft
Sent: Friday, 4 November 2005 10:34 a.m.
To: tutor at python.org
Subject: [Tutor] File IO


Hello,
     I tried the following code:

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


print "Here goes"
print "Enter filename:"
filename = raw_input("Name:")
print readSOMNETM(filename)
print "might work"

on  a file that lookes like this:

Monthly Weather Data, LAU73M.MET, converted from:
BAD LAUCHSTAEDT; DAILY METEOROLOGICAL DATA FOR 01/01/1973-31/12/1973 VAP,
DEWP CALCULATED FROM MEASURED AVTEMP AND HUMID DATA
MONTH	RAIN	AVTEMP	S10	RAD	SUN	WIND	EVAPW
**************************************************************
1	22.5	0.3	*	54.6	15.1	*	11.9
2	16.1	1.8	*	110	51.1	*	18.1
3	16.4	4.8	*	227.5	94.5	*	36.8
4	19.5	5.9	*	286.3	89	*	45.5
5	36.1	13.2	*	448.5	164.6	*	83
6	36	16.9	*	525.7	208.8	*	105.7
7	37.7	18.2	*	459.7	165.4	*	98.6
8	29.3	18.2	*	463.8	206.8	*	97.9
9	27	14.8	*	277.5	119.5	*	58.7
10	57.6	7.6	*	158.7	72.2	*	31.3
11	23.4	3.9	*	98.3	75.6	*	19.1
12	14	0.7	*	55.5	38	*	12.5


And recieved the following error:

Traceback (most recent call last):
  File "C:\Python24\INProgress.py", line 15, in -toplevel-
    print readSOMNETM(filename)
  File "C:\Python24\INProgress.py", line 6, in readSOMNETM
    data = fields[1] + fields[2] + fields[7]
IndexError: list index out of range

Essentially I'm trying to write a bit of code that can take any of the
fields in the above data i.e. rain temp evap for each month for a hundred or
so files like this one and spit out a file at the end that has the data in a
different format.

Any help would be very much appreciated I need to get this done by the end
of next week

Thanks

Mike

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

A new monthly electronic newsletter covering all aspects of MED's work is now available.  Subscribers can choose to receive news from any or all of seven categories, free of charge: Growth and Innovation, Strategic Directions, Energy and Resources, Business News, ICT, Consumer Issues and Tourism.  See http://news.business.govt.nz for more details.




http://www.govt.nz - connecting you to New Zealand central & local government services

Any opinions expressed in this message are not necessarily those of the Ministry of Economic Development. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer.


More information about the Tutor mailing list