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

10.2

By: dvdjaco on Feb 18th, 2012  |  syntax: Python  |  size: 0.54 KB  |  hits: 22  |  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. # By dvdjaco
  4. # Exercise 10.2
  5. #
  6.  
  7. fname = raw_input('Enter a filename: ')
  8.  
  9. try:
  10.     f = open(fname)
  11. except err:
  12.     print "There was an error: ", err
  13.    
  14. d = dict()
  15.    
  16. for line in f:
  17.     words = line.split()
  18.     if len(words) == 0 or words[0] != 'From' or words[1].find('@') == -1: continue
  19.     time = words[5]
  20.     hour = time.split(':')[0]
  21.     d[hour] = d.get(hour,0) + 1
  22.  
  23. lst = list()
  24. for hour, count in d.items(): lst.append( (hour, count) )
  25. lst.sort()
  26. for hour,count in lst: print hour, count