
9-3
By:
Mars83 on
Oct 9th, 2011 | syntax:
Python | size: 1.24 KB | hits: 29 | expires: Never
#! /usr/bin/env python3.2
# -*- coding: utf-8 -*-
# main.py
""" Task: Exercise 9.3
Write a program that categorizes each mail message by which day of the week
the commit was done. To do this look for lines which start with “From”,
then look for the third word and then keep a running count of each of the
days of the week. At the end of the program print out the contents of your
dictionary (order does not matter).
Sample Line:
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Sample Execution:
python dow.py
Enter a file name: mbox-short.txt
{'Fri': 20, 'Thu': 6, 'Sat': 1}
"""
''' Main '''
# Open file
try:
fhand = open('mbox-short.txt')
except:
print("File not found!")
exit()
week = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
days = dict()
for line in fhand:
words = line.split()
# print('Debug:', words)
if len(words) >= 3 and words[0] == 'From' and words[2] in week[:]:
days[words[2]] = days.get(words[2], 0) + 1
for i in week: # Print-out
if i in days:
print(str(i) + ": " + str(days[i]))
else:
print(str(i) + ": 0")
# Close file
try:
fhand.close()
except:
exit()