Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement