Advertisement
Guest User

Untitled

a guest
May 17th, 2012
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # P2PU - Python Programming 101 - Chapter 9 - Working with Files
  4. # Python for Informatics - Chapter 7 - Files
  5. # Exercise 7.2
  6.  
  7. print '\n# Exercise 7.2\n'
  8. fname =  raw_input('Enter a file name: ')
  9. try:
  10.     ffile = open(fname)
  11. except:
  12.     print 'File', fname, 'cannot be opened!'
  13.     exit()
  14. count = 0
  15. error = 0
  16. dspam = 0.0
  17. confi = 0
  18. for line in ffile:
  19.     if line.find('X-DSPAM-Confidence:') == -1:
  20.         continue
  21.     try:
  22.         val = line[line.find(':')+1:]
  23.         val = val.strip()
  24.         dspam += float(val)
  25.         count += 1
  26.     except:
  27.         error += 1
  28.         continue
  29. print line.upper()
  30. if error > 0: print 'There were %d conversion errors' % error
  31. if count > 0: confi = float(dspam/count)
  32. print 'Average spam confidence: %g' % confi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement