Woah, that's way simpler than mine.

Liam Clarke-Hutchinson


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of bob
Sent: Friday, 4 November 2005 11:01 a.m.
To: [EMAIL PROTECTED]; tutor@python.org
Subject: Re: [Tutor] File IO


At 01:34 PM 11/3/2005, Michael Haft wrote:
>Hello,
>      I tried the following code:
>
>def readSOMNETM(inputName):
>     input = open(inputName, "r")
>     result = []
>     for line in input:
>         fields = line.split()

           # add this; it will show you what line(s) have less than 8 fields
           if len(fields) < 8:
               print "Line too short", line
               continue

>         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

_______________________________________________
Tutor maillist  -  Tutor@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.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to