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.
Question
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.
Solution
Here is a Python solution for your problem:
count = 0
with open('mbox-short.txt', 'r') as f:
for line in f:
if line.startswith('From '):
words = line.split()
print(words[1])
count += 1
print('There were', count, 'lines in the file with From as the first word')
This script works as follows:
- It initializes a counter to 0.
- It opens the file 'mbox-short.txt' in read mode.
- It reads the file line by line.
- If a line starts with 'From ', it splits the line into words.
- It prints the second word in the line, which is the email address.
- It increments the counter.
- After all lines have been processed, it prints the count of lines that started with 'From '.
Similar Questions
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.
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.
To display number of Recipients from each City. Also write the output
Find the count of letters and words in a file.
Write a Program to extract the user id, domain name, and suffix from the following email addresses.Input format :The input consists of the email id.Output format :The output prints the user id, domain name, and suffix.Sample test cases :Input 1 :[email protected] [email protected] [email protected] 1 :zuck26 facebook compage33 google comjeff42 amazon com
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.