Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 17th, 2012  |  syntax: Python  |  size: 0.73 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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