Advertisement
Guest User

chapter 7 exercises

a guest
Nov 7th, 2013
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. Exercise 7.1
  2. # -*- coding: iso-8859-15 -*-
  3. fasa = open('/home/fer/mbox-short.txt')
  4. for linea in fasa:
  5.     print linea.upper()
  6. fasa.close()               #just to get used to it
  7.  
  8. Exercise 7.2
  9. # -*- coding: iso-8859-15 -*-
  10. nombref = raw_input("Nombre de archivo: ")
  11. try:
  12.     fasa = open(nombref)
  13.     count=0
  14.     total=0
  15.     num=0
  16.     for linea in fasa:
  17.         if linea.startswith("X-DSPAM-Confidence:"):
  18.             num = linea[20:26]
  19.             num = float(num)
  20.             total = total + num
  21.             count +=1
  22.     print "Total sum ",  total
  23.     print "Number of lines ",  count
  24. except:
  25.     print "file %s can not be opened" % nombref
  26.  
  27. Exercise 7.3
  28. # -*- coding: iso-8859-15 -*-
  29.  
  30. nombref = raw_input("Nombre de archivo: ")
  31. try:
  32.     fasa = open(nombref)
  33.     count=0
  34.     total=0
  35.     num=0
  36.    
  37.     for linea in fasa:
  38.         if linea.startswith("X-DSPAM-Confidence:"):
  39.             num = linea[20:26]
  40.             num = float(num)
  41.             total = total + num
  42.             count +=1
  43.            
  44.     print "Total sum ",  total
  45.     print "Number of lines ",  count
  46.  
  47. except:
  48.     if nombref =='na na boo boo':
  49.         print "NA NA BOO BOO TO YOU - You have been punk'd!"
  50.     else:
  51.         print "file %s can not be opened" % nombref
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement