Facebook
From Idiotic Treeshrew, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 175
  1. #Exercise 2: Write a program that categorizes each mail message by
  2. # which day of the week the commit was done. To do this look for lines
  3. # that start with “From”, then look for the third word and keep a running
  4. # count of each of the days of the week. At the end of the program print
  5. # out the contents of your dictionary (order does not matter).
  6. #Sample Line:
  7. #From [email protected] Sat Jan 5 09:14:16 2008
  8. count = 0
  9. domains = dict()
  10. senders = dict()
  11. days = dict()
  12. fhandle = open("mbox-short.txt", "r")
  13. for line in fhandle:
  14.     count = count + 1
  15.     if line.startswith("From"):
  16.         line = line.rstrip()
  17.         words = line.split()
  18.         sender = words[1]
  19.         senderinfos = sender.split("@")
  20.         senderdomain = senderinfos[1]
  21. #        print(repr(line), repr(words))
  22.         day = words[2]
  23.         days[day] = days.get(day, 0) + 1
  24.         senders[sender] = senders.get(sender, 0) + 1
  25.         domains[senderdomain] = domains.get(senderdomain, 0) + 1
  26. print(days)
  27. #print(max(senders.keys()), max(senders.values()))
  28. #print(domains)
  29. print(count)