Extract value and average

Rhodri James rhodri at wildebst.demon.co.uk
Mon Jun 8 18:57:41 CEST 2009


On Mon, 08 Jun 2009 17:29:12 +0100, Steven D'Aprano  
<steve at remove-this-cybersource.com.au> wrote:

> On Mon, 08 Jun 2009 18:13:50 +0200, Francesco Pietra wrote:
>
>> I come 'naked', which is unusual and unfair.
>
> ???

Yeah, I had to go and scrub my brain out :-)

> Assuming no DIHED value will ever be split over two lines:
>
>
> data = open(filename)
> values = []
> for line in data:
>     if line and line.strip():  # ignore blanks
>          words = line.strip().split()
>          try:
>              i = words.index("DIHED")
>          except IndexError:

Should be ValueError, not IndexError.

>              continue
>          values.append(float(words[i+2]))
> mean = sum(values)/len(values)
>
>
> should do the job.

You don't need the second strip(),
         words = line.split()
should be sufficient.

(And we're back to the 'naked' bit again...)

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list