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.
Question
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.
Solution
Here is a Python program that accomplishes the task:
# Open the file
file = open('mbox-short.txt', 'r')
# Initialize an empty dictionary
senders = {}
# Loop through each line in the file
for line in file:
# If the line starts with 'From '
if line.startswith('From '):
# Split the line into words
words = line.split()
# The second word is the sender's email
sender = words[1]
# If the sender is already in the dictionary, increment their count
if sender in senders:
senders[sender] += 1
# Otherwise, add the sender to the dictionary with a count of 1
else:
senders[sender] = 1
# Initialize the maximum count and prolific sender
max_count = None
prolific_sender = None
# Loop through each sender in the dictionary
for sender, count in senders.items():
# If this sender's count is higher than the current maximum
if max_count is None or count > max_count:
# Update the maximum count and prolific sender
max_count = count
prolific_sender = sender
# Print the most prolific sender and their count
print(prolific_sender, max_count)
This program reads through the file line by line, and for each line that starts with 'From ', it splits the line into words and takes the second word as the sender's email. It then updates the count for that sender in the dictionary. After going through all the lines, it finds the sender with the maximum count and prints their email and count.
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 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.
Your boss sent you an important account code by mail two months ago. Unfortunately, you haven't been sorting or deleting your mail and there are currently over 41,000 mails in your Inbox. You need that account code now. What is the quickest way to find it?OptionsSort by date and then look through mail from two months agoLook through the "Important mail from my boss" folderSort by "From" and then look at all of the mail from your bossSearch on key words related to the account code and your boss's name
To display number of Recipients from each City. Also write the output
For this lab, imagine you are an IT Specialist at a medium-sized company. The Human Resources Department at your company wants you to find out how many people are in each department. You need to write a Python script that reads a CSV file containing a list of the employees in the organization, counts how many people are in each department, and then generates a report using this information. The output of this script will be a plain text file.
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.