collecting data from file

Peter Hansen peter at engcorp.com
Fri Apr 11 10:31:32 EDT 2003


Terry Reedy wrote:
> 
> >  Also, are there any error conditions to be handled, or do
> > you guarantee the input is always perfect (e.g., always at least four
> > columns, and so on)?
> 
> Missing data is the great bugaboo of data analysis.  Ignoring that
> possibility
> 
> sum = 0
> for line in file('mydata'):
>   fields = line.split() # assume whitespace
>   if field[1] == 'HELLO':
>     sum += field[3]
>   elif field[1] == 'BYE'
>     sum += field[3]

I believe the last line was intended to read

      sum -= field[3]

Terry, I like how you solution *exactly* matches the requirements
as specified, by not even printing the resulting sum.  Very 
efficient of you.  :-)

-Peter




More information about the Python-list mailing list