#!/usr/bin/env python import sys,string count = 0 sum = 0.0 ignore=0 def usage(retval): sys.stderr.write('Usage: %s [-i]\n' % sys.argv[0]) sys.stderr.write('-i says to ignore lines that cannot be converted to a floating point number - IE, do not error out\n') sys.exit(retval) while sys.argv[1:]: if sys.argv[1] == '-i': ignore=1 del sys.argv[1] elif sys.argv[1] == '-h': del sys.argv[1] usage(0) else: usage(1) while 1: line = sys.stdin.readline() if not line: break try: t = string.atof(string.strip(line)) except: sys.stderr.write('Could not convert %s to a float\n' % string.strip(line)) if not ignore: sys.exit(1) else: count = count + 1 sum = sum + t if count == 0: print 0 else: print sum/count