-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path10_exercise_10_2.py
More file actions
23 lines (21 loc) · 873 Bytes
/
10_exercise_10_2.py
File metadata and controls
23 lines (21 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Exercise 10.2
10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.
From [email protected] Sat Jan 5 09:14:16 2008
Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.
"""
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
hours = {}
for line in handle:
if "From:" in line: continue
elif "From" in line:
tmp = line.split( )
tmp = str(tmp[5]).split(":")
if tmp[0] not in hours:
hours[tmp[0]] = 1
else: hours[tmp[0]]=hours.get(tmp[0],0) + 1
else: continue
for k,v in sorted(hours.items()):
print(k,v)