I was given an assignment in class that is making my head spin. We are given a txt file that is a template for "emails," with two f string values in it that need to have the f string values be replaced by data from a csv file and printed to the console.
To: {email}
From: noreply@deals.com
Subject: Deals!
Hi {first_name},
We've got some great deals for you. Check our website!
The tricky part is, we are not allowed to paste the contents of the txt file as a string literal, but have to use the template in our code.
So far, I am only printing out the correct number of emails for how much data there is but the f strings are not being filled in. I have tried writing this program in a number of ways at this point. Any help would be appreciated.
"""This program reads an email template and csv file,\n
with names and generates emails based on these files."""
import csv
"""This function opens the template, and fills in the f strings with data from the csv file."""
def create_email():
try:
a = open("email_template.txt")
template_mail = a.read()
with open("emails.csv") as f:
reader = csv.reader(f, delimiter =',')
for row in reader:
email = row[2]
first_name = row[0]
print(template_mail)
except Exception as e:
print("Error: ", e)
finally:
a.close
create_email()
NOTE that I am NOT trying to write to the txt file, only print its contents with the data from the csv file to the console.
I have tried using (template_mail.format(row[2], row[0])) to format the f strings with values from the csv file, no luck. I created a seperate function at first to open the txt file which called inside of the "create_email" function. Result is the same as the code I provided. I need the console to print out the template provided with f strings filled in with data from the provided csv file. I expected my solutions to do this from all the digging I have done online but nothing has worked.