Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function to read the list of email addresses from a file. Each line of t

ID: 3574300 • Letter: W

Question

Write a function to read the list of email addresses from a file. Each line of the file contains a single email address with user and host separated by an at sign('@') character. The function will be given the filename and will return a list of lists. Each element of the list is a list with two values: user name (string) and host name (string). You must parse each line, partition into parts, and string white space from each entry. You must handle when the file does not exist and return an empty list Use the following function definition def myBuild(filename):

Explanation / Answer

def myBuild(filename):
ouput = []; # ouput list of lists
try: # try block
f = open(filename, 'r') # opening the file
r = f.readlines() # reading lines
for i in range(len(r)): # for each line parsing user and host
x = []
splChar = r[i].find("@")
user = r[i][:splChar].strip() # trimming spaces
host = r[i][splChar+1:-1].strip()
x.append(user) # appending it to temporary list
x.append(host) # appending it to temporary list
ouput.append(x) # appenidng temporary list to ouput list of lists
return ouput # returning the output
except IOError:
return ouput # returing empty list incase file doesn't exist
  

#Sample Input

#chegg@ chegg.com
#cheggIndia @chegg.com
#hurr@randommail.com

# sample rus

print myBuild('Newfile.py');
# [['chegg', 'chegg.com'], ['cheggIndia', 'chegg.com'], ['hurr', 'randommail.com']]
print myBuild('invalid.py');
# []

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote