
Untitled
By: a guest on
May 17th, 2012 | syntax:
Python | size: 0.73 KB | hits: 35 | expires: Never
#!/usr/bin/python
# P2PU - Python Programming 101 - Chapter 9 - Working with Files
# Python for Informatics - Chapter 7 - Files
# Exercise 7.2
print '\n# Exercise 7.2\n'
fname = raw_input('Enter a file name: ')
try:
ffile = open(fname)
except:
print 'File', fname, 'cannot be opened!'
exit()
count = 0
error = 0
dspam = 0.0
confi = 0
for line in ffile:
if line.find('X-DSPAM-Confidence:') == -1:
continue
try:
val = line[line.find(':')+1:]
val = val.strip()
dspam += float(val)
count += 1
except:
error += 1
continue
print line.upper()
if error > 0: print 'There were %d conversion errors' % error
if count > 0: confi = float(dspam/count)
print 'Average spam confidence: %g' % confi