Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 14:44
Show Gist options
  • Save MichelleDalalJian/cdf4588114348d50700a30cd7d37277c to your computer and use it in GitHub Desktop.
Save MichelleDalalJian/cdf4588114348d50700a30cd7d37277c to your computer and use it in GitHub Desktop.
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 c…
name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
hand = open(name)
hours = dict()
for line in hand:
if line.startswith("From "):
hour = line.split()[5].split(':')[0]
hours[hour] = hours.get(hour, 0) + 1
for k, v in sorted(hours.items(), None):
print (k,v)
@VijendraSharma
Copy link

hour = line.split()[5].split(':')[0]
hours[hour] = hours.get(hour, 0) + 1

Please explain how it working..

@luqkrzy
Copy link

luqkrzy commented Oct 2, 2020

fname = input("Enter file: ")
if len(fname) < 1 : fname = "mbox-short.txt"
handle = open(fname)

li = list()
for line in handle:
    if not line.startswith('From '): continue
    hours = line.split()[5].split(":")[0]
    li.append(hours)

di = {i:li.count(i) for i in li}

for key, value in sorted(di.items()):
    print(key, value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment