Marcatectura Marcatectura - 1 month ago 5
Python Question

How to replace substrings like NAME in a string template?

I've got a string containing substrings I'd like to replace, e.g.

text = "Dear NAME, it was nice to meet you on DATE. Hope to talk with you and SPOUSE again soon!"


I've got a csv of the format (first row is a header)

NAME, DATE, SPOUSE
John, October 1, Jane
Jane, September 30, John
...


I'm trying to loop through each row in the csv file, replacing substrings in
text
with the csv element from the column with header row matching the original substring. I've got a list called
matchedfields
which contains all the fields that are found in the csv header row and
text
(in case there are some columns in the csv I don't need to use). My next step is to iterate through each csv row and replace the matched fields with the element from that csv column. To accomplish this, I'm using

with open('recipients.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
for match in matchedfields:
print inputtext.replace(match, row[match])


My problem is that this only replaces the first matched substring in
text
with the appropriate element from the csv. Is there a way to make multiple replacements simultaneously so I end up with

"Dear John, it was nice to meet you on October 1. Hope to talk with you and Jane again soon!"

"Dear Jane, it was nice to meet you on September 30. Hope to talk with you and John again soon!"

Answer

I think the real way to go here is to use string templates. Makes your life easy.

Here is a general solution that works under Python2 and 3:

import string
templateText = string.Template("Dear ${NAME}, it was nice to meet you on ${DATE}. Hope to talk with you and ${SPOUSE} again soon!")

And then

import csv
with open('recipients.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(templateText.safe_substitute(row))

Now, I noticed that your csv is kind of messed up with whitespaces, so you'll have to take care of that first (or adapt the call to either the csv reader or the template).

Comments