-2

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.

  • Does this answer your question? [How do I convert a string into an f-string?](https://stackoverflow.com/questions/47339121/how-do-i-convert-a-string-into-an-f-string) – esqew Aug 08 '23 at 02:53

1 Answers1

0

Let's start with the template:

To:      {email}
From:    noreply@deals.com
Subject: Deals!

Hi {first_name},

We've got some great deals for you. Check our website!

...and a small CSV file:

Bill,Gates,bill@microsoft.com
John,Doe,john@gmail.com

...then...

from pathlib import Path
import csv

BASE = Path('/Volumes/G-Drive')

with open(BASE.joinpath('email_template.txt')) as template:
    ts = template.read()
    with open(BASE.joinpath('emails.csv')) as emails:
        for first_name, _, email in csv.reader(emails):
            print(ts.format(email=email, first_name=first_name))

Output:

To:      bill@microsoft.com
From:    noreply@deals.com
Subject: Deals!

Hi Bill,

We've got some great deals for you. Check our website!

To:      john@gmail.com
From:    noreply@deals.com
Subject: Deals!

Hi John,

We've got some great deals for you. Check our website!
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • Thank you! I am able to get this to work without using the pathlib module as well given the teacher assumes both files are in the same directory as the python script. If you could elaborate as to why the way you format the print method works that would be awesome. – AnthonyAtHome Aug 08 '23 at 16:50
  • @AnthonyAtHome It's all in the documentation here: https://docs.python.org/3/tutorial/inputoutput.html – DarkKnight Aug 08 '23 at 16:54