Geeks of Coding

Join us on Telegram

Home Forums Assignment courserra Python for Everybody – Specialization Python Data Structures Assignment 10.2, Python for Data Structure

Viewing 0 reply threads
  • Author
    • #820
      Abhishek TyagiAbhishek Tyagi

      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 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

      #Use mbox-short.txt File name
      name = input("Enter file:")
      f = open(name)
      dic = {}
      for i in f:
          if i.startswith("From") and len(i.split()) > 2:
              line = i.split()
              if not dic.has_key(line[5][:2]):
                  dic[line[5][:2]] = 1
                  dic[line[5][:2]] += 1
      key = sorted(dic)
      for i in key:
          print (i, dic[i])
Viewing 0 reply threads
  • You must be logged in to reply to this topic.