-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathexercise9_2.py
executable file
·38 lines (32 loc) · 1.15 KB
/
exercise9_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
"""
Exercise 9.2: Write a program that categorizes each mail message by which day
of the week the commit was done. To do this, look for lines that start with
"From", then look for the third word and 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 [email protected] 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}
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
"""
dictionary_days = dict() # Initializes the dictionary
fname = input('Enter a file name: ')
try:
fhand = open(fname)
except FileNotFoundError:
print('File cannot be opened:', fname)
exit()
for line in fhand:
words = line.split()
if len(words) < 3 or words[0] != 'From':
continue
else:
if words[2] not in dictionary_days:
dictionary_days[words[2]] = 1 # First entry
else:
dictionary_days[words[2]] += 1 # Additional counts
print(dictionary_days)