Advertisement
Guest User

P2PU Exercises Chapter 7

a guest
Mar 29th, 2013
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. """
  2. Created on Wed Mar 27 18:10:31 2013
  3. Exercise 7.1 shout.py
  4. @author: casa
  5. """
  6. #f = open('mbox-short.txt', 'r')
  7. fname = raw_input("Enter a file name: ")
  8. try :
  9.     f = open(fname)
  10. except :
  11.     print "The file %s doesn't exist or could not be opened." % fname
  12. smalltext = f.readlines()
  13. for line in smalltext :
  14.     line = line.rstrip()
  15.     print line.upper()
  16. f.close()
  17. exit()
  18.  
  19. """
  20. Created on Fri Mar 29 18:36:13 2013
  21. Exercise 7.2 SPAM confidence
  22. @author: casa
  23. """
  24. #f = open('mbox-short.txt', 'r')
  25. fname = raw_input("Enter a file name: ")
  26. try :
  27.     f = open(fname)
  28. except :
  29.     print "The file %s doesn't exist or could not be opened." % fname
  30. l = list()
  31. for line in f :
  32.     if line.startswith('X-DSPAM-Confidence') :
  33.         value = line[20:26]
  34.         l.append(float(value))    
  35. print ("The average SPAM confidence is: "), sum(l)/len(l)
  36. exit()
  37.  
  38.  
  39. """
  40. Created on Fri Mar 29 18:36:13 2013
  41. Exercise 7.3
  42. @author: casa
  43. """
  44. #f = open('mbox-short.txt', 'r')
  45. fname = raw_input("Enter a file name: ")
  46. if fname == "na na boo boo" :
  47.         print "Do you think it's Eastern this weekend?"
  48.         exit()
  49. try :
  50.     f = open(fname)
  51. except :
  52.     print "The file %s doesn't exist or could not be opened." % fname
  53. count = 0
  54. for line in f :
  55.     if line.startswith("Subject") :
  56.         count = count + 1    
  57. print ("There were %d subject lines in %s: ") % (count, fname)
  58. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement