In: Computer Science
Python 3
8.5 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 2008 You 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:'. You can download the sample data at http://www.py4e.com/code3/mbox-short.txt
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
#opening mbox-short.txt file, make sure you have the file in
same directory
file=open('mbox-short.txt')
#count of emails extracted
count=0
#looping through each line in file
for line in file:
#checking if line starts with 'From '
if
line.startswith('From '):
#splitting line into
a list
fields=line.strip().split()
#ensuring that there
is at least 2 elements in resultant list
if
len(fields)>=2:
#extracting 2nd value as email
email=fields[1]
#printing it
print(email)
#updating count
count+=1
#closing file
file.close()
#displaying count. note that this count is including the
duplicates (if any)
#if you want to count only unique addresses, let me
know.
print(count,'addresses were extracted!')
#output
27 addresses were extracted!