Skip to content

Commit 4d8f564

Browse files
Create Assignment 10.2.py
1 parent b6c8df1 commit 4d8f564

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Week 6/Assignment 10.2.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#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.
2+
#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.
3+
#From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
4+
#Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.
5+
6+
7+
8+
9+
name = raw_input("Enter file:")
10+
if len(name) < 1 : name = "mbox-short.txt"
11+
handle = open(name)
12+
d=dict()
13+
for line in handle:
14+
if not line.startswith("From "):
15+
continue
16+
else:
17+
line=line.split()
18+
line=line[5]
19+
line=line[0:2]
20+
d[line]=d.get(line,0)+1
21+
22+
lst=list()
23+
for value,count in d.items():
24+
lst.append((value,count))
25+
26+
lst.sort()
27+
for value,count in lst:
28+
print (value,count)

0 commit comments

Comments
 (0)