Advertisement
Mars83

9-3

Oct 9th, 2011
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #! /usr/bin/env python3.2
  2. # -*- coding: utf-8 -*-
  3.  
  4. # main.py
  5. """ Task: Exercise 9.3
  6.    Write a program that categorizes each mail message by which day of the week
  7.    the commit was done. To do this look for lines which start with “From”,
  8.    then look for the third word and then keep a running count of each of the
  9.    days of the week. At the end of the program print out the contents of your
  10.    dictionary (order does not matter).
  11.    
  12.    Sample Line:
  13.    From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
  14.    
  15.    Sample Execution:
  16.    python dow.py
  17.    Enter a file name: mbox-short.txt
  18.    {'Fri': 20, 'Thu': 6, 'Sat': 1}
  19. """
  20.  
  21. ''' Main '''
  22. # Open file
  23. try:
  24.     fhand = open('mbox-short.txt')
  25. except:
  26.     print("File not found!")
  27.     exit()
  28.  
  29. week = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  30. days = dict()
  31. for line in fhand:
  32.     words = line.split()
  33.     # print('Debug:', words)
  34.     if len(words) >= 3 and words[0] == 'From' and words[2] in week[:]:
  35.         days[words[2]] = days.get(words[2], 0) + 1
  36.  
  37. for i in week:          # Print-out
  38.     if i in days:
  39.         print(str(i) + ": " + str(days[i]))
  40.     else:
  41.         print(str(i) + ": 0")
  42.  
  43. # Close file
  44. try:
  45.     fhand.close()
  46. except:
  47.     exit()
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement