Write a function that given a list of emails, writes the emails to a file called
ID: 3702892 • Letter: W
Question
Write a function that given a list of emails, writes the emails to a file called "emails.csv"
Add a header of your choosing to your file as the first line.
Your file, emails.csv, will look like this:
list_of_emails
bellamys@mail.med.upenn.edu
warren@upenn.edu
bryanma@upenn.edu
#!/bin/python3
import sys
import os
# Complete the function below.
def write_to_csv(list_of_emails):
#write code that should be here
emails = list(map(str.strip, sys.stdin.readlines()))
write_to_csv(emails)
assert os.path.exists('emails.csv'), 'did you write to "emails.csv"?'
with open('emails.csv', 'r') as f:
header = f.readline()
emails2 = []
for line in f.readlines():
emails2.append(line.strip())
os.remove('emails.csv')
assert all(i == j for i, j in zip(emails, emails2)), 'this list of emails is different'
assert len(emails) == len(emails2), 'this list of emails is different'
print(1)
Explanation / Answer
#!/bin/python3 import sys import os # Complete the function below. def write_to_csv(list_of_emails): with open('emails.csv', 'w') as f: f.write('list_of_emails ') for email in list_of_emails: f.write(email+' ') emails = list(map(str.strip, sys.stdin.readlines())) write_to_csv(emails) assert os.path.exists('emails.csv'), 'did you write to "emails.csv"?' with open('emails.csv', 'r') as f: header = f.readline() emails2 = [] for line in f.readlines(): emails2.append(line.strip()) os.remove('emails.csv') assert all(i == j for i, j in zip(emails, emails2)), 'this list of emails is different' assert len(emails) == len(emails2), 'this list of emails is different' print(1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.