Knowee
Questions
Features
Study Tools

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 2008Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

Question

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 2008Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python program that reads through the mbox-short.txt file and figures out the distribution by hour of the day for each of the messages:

counts = dict()
try:
    fhand = open('mbox-short.txt')
except:
    print('File cannot be opened:')
    exit()

for line in fhand:
    if not line.startswith('From '): continue
    pieces = line.split()
    time = pieces[5]
    hour = time.split(':')[0]
    counts[hour] = counts.get(hour, 0) + 1

# Sort by hour
sorted_counts = sorted([(k,v) for k,v in counts.items()])

for hour, count in sorted_counts:
    print(hour, count)

This program works as follows:

  1. It first tries to open the file 'mbox-short.txt'. If the file cannot be opened, it prints an error message and exits.
  2. It then reads through each line in the file.
  3. If a line does not start with 'From ', it skips to the next line.
  4. If a line does start with 'From ', it splits the line into pieces using spaces as the delimiter.
  5. It then gets the time from the 6th piece (index 5) and splits the time into hours, minutes, and seconds using a colon as the delimiter.
  6. It gets the hour (the first piece of the time split) and increments the count for that hour in the counts dictionary.
  7. After going through all the lines, it sorts the counts dictionary by hour.
  8. Finally, it prints the counts for each hour, sorted by hour.

This problem has been solved

Similar Questions

Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:From [email protected] Sat Jan 5 09:14:16 2008You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.

Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer.

Select the correct answerFind median and mode of the messages received on 9 consecutive days 15, 11, 9, 5, 18, 4, 15, 13, 17.Options18, 1513, 1813, 615, 16

Find median and mode of the messages received on 9 consecutive days 15, 11, 9, 5, 18, 4, 15, 13, 17.

Find median and mode of the messages received on 9 consecutive days 15, 11, 9, 5, 18, 4, 15, 13, 17.Options15, 1613, 1813, 618, 15

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.