0% found this document useful (0 votes)
213 views1 page

Assign8 5

This document provides instructions to open a text file called mbox-short.txt, read it line by line, and parse any lines that start with "From " to print the second word of the line which contains the email address. It also provides a sample count of the number of lines that start with "From ".

Uploaded by

KarthikPillai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views1 page

Assign8 5

This document provides instructions to open a text file called mbox-short.txt, read it line by line, and parse any lines that start with "From " to print the second word of the line which contains the email address. It also provides a sample count of the number of lines that start with "From ".

Uploaded by

KarthikPillai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

'''

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.pythonlearn.com/code/mbox-short.txt


'''

fname = raw_input("Enter file name: ")


if len(fname) < 1 : fname = "mbox-short.txt"

fh = open(fname)
count = 0

for line in fh:


line.rstrip()
if line.startswith("From "):
print line.split()[1]
count =count + 1

print "There were", count, "lines in the file with From as the first word"

You might also like