PERSPECTIVE: We need practice in using files and strings and we don\'t have much
ID: 3909010 • Letter: P
Question
PERSPECTIVE: We need practice in using files and strings and we don't have much time Thus this is a short assignment. PROBLEM BACKGROUND: Assume you're working on a contract where a company is building a mailing list (or, rather, an e-mailing list) by analyzing e-mail messages. Your task is to write a Python program that reads a file (stored in the current working directory) called mail.dat and outputs to a file called addresses.dat, one per line, every e-mail address containe inside the file. (You can see now why this assignment is titled "The Spammer's Delight Problem") For this assignment, if a whitespace delimited string of characters has an embedded commercial at sign (a) inside it (that is, interior to the string), we shall consider it an e-mail address. However, any trailing commas must be trimmed from addresses. Thus the string "abc@mtsu.edu," must appear in the output file as "abcamtsu.edu" with the trailing comma removed. Only commas at the end of a string are considered trailing; do not remove non-trailing commas. Do not worry about any other punctuation characters; the only editing your program must do is to remove trailing commas IMPLEMENTATION NOTES: Use as your source file name "spammer.py". Don't forget to head your source file with the standard title line and global comments we use for all programming assignments. Be sure to have code to close0 both files that you opened INPUT/OUTPUT: You should create your own data file(s) to initially test your program. For grading, link the file SPUB/mail.dat into your work area and use it. The command ln-s ?PUB/mail.dat mail.dat will do that. (Be sure to link not copy the file into your work area.) For your output file, use the filename addresses.dat. Do not write anything to standard output (i.e., do not use any print0 function calls in your final product). The first 10 lines of your addresses.dat file should look like mikkifloweregmail.com mikkifloweregmail.com infolacademart.edu alvarolancharteapanimationschool.com dst@asu.edu mthomasearacademy.edu sberkaneartcenter.eduExplanation / Answer
import re
def isValidEmail(email):
#start of FUN"
#reg expression for email validation
if re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$)", email) != None:
#start of if"
return True
#end of if
return False
#end of fun
f=open("mail.dat","r")
x=f.readline()#readline from file
g=open("addresses.dat","a")
while(x):#while EOF is reached
#start of while
data = x.split(" ")#split line by whitespace
l=len(data)#find length of words
for i in range(l):
#start of for
email=data[i].strip(",").strip()
if(email[0]=='<' and email[-1]=='>'):
#start of if
email=email[1:-1].strip(",").strip()
if(isValidEmail(email)):
#start of if
g.write(email+" ")
#end of if
#end of if
else:
#start of else
if(isValidEmail(email)):
#start of if
g.write(email+" ")
#end of if
#end of else
x=f.readline()
#end of while
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.