Steven D'Aprano wrote:
...
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()
words = line.split() # does the same as above
try:
i = words.index("DIHED")
except IndexError:
continue
values.append(float(words[i+2]))
mean = sum(values)/len(values)
Or similarly (with error checks):
values = []
with open('/imports/file.txt') as data:
for line in data:
parts = line.split(' DIHED ') # assume spaces around DIHED
if len(parts) > 1:
if len(parts) > 2: # make sure only one DIHED found
raise ValueError('Line has DIHED twice: %s' % line)
# break the post-DIHED part into '=', <value>, <rest>
words = parts[1].split(None, 2)
values.append(float(tokens[1]))
mean = sum(values) / len(values)
--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list