Python 如何在字符串模板中替换像NAME这样的子字符串?

Python 如何在字符串模板中替换像NAME这样的子字符串?,python,replace,substring,template-strings,Python,Replace,Substring,Template Strings,我有一个包含要替换的子字符串的字符串,例如 text = "Dear NAME, it was nice to meet you on DATE. Hope to talk with you and SPOUSE again soon!" 我得到了该格式的csv(第一行是标题) 我正在尝试循环浏览csv文件中的每一行,将text中的子字符串替换为列中的csv元素,其标题行与原始子字符串匹配。我有一个名为matchedfields的列表,其中包含在csv标题行和文本中找到的所有字段(如果csv中

我有一个包含要替换的子字符串的字符串,例如

text = "Dear NAME, it was nice to meet you on DATE. Hope to talk with you and SPOUSE again soon!"
我得到了该格式的csv(第一行是标题)

我正在尝试循环浏览csv文件中的每一行,将
text
中的子字符串替换为列中的csv元素,其标题行与原始子字符串匹配。我有一个名为
matchedfields
的列表,其中包含在csv标题行和
文本中找到的所有字段(如果csv中有一些列我不需要使用)。我的下一步是遍历每个csv行,并用该csv列中的元素替换匹配的字段。为了实现这一点,我正在使用

with open('recipients.csv') as csvfile:
 reader = csv.DictReader(csvfile)
 for row in reader:
     for match in matchedfields:
        print inputtext.replace(match, row[match])
我的问题是,这只会用csv中的适当元素替换
text
中第一个匹配的子字符串。有没有一种方法可以同时进行多次替换,这样我就可以

"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!"

您应该将替换的字符串重新指定给原始名称,以便不会丢弃以前的替换:

with open('recipients.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        inputtext = text
        for match in matchedfields:
            inputtext = inputtext.replace(match, row[match])
        print inputtext
另一方面,您可以使用字符串格式更新原始字符串,只需对字符串稍作修改,如下所示:

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

with open('recipients.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        inputtext = text.format(row)
        print inputtext

一步使用字典格式化字符串,而无需迭代替换。

问题在于
inputtext.replace(match,row[match])
不会更改
inputtext
变量,它只会生成一个新的字符串,而不存储该字符串。试试这个:

import copy 

with open('recipients.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        inputtext_copy = copy.copy(inputtext) ## make a new copy of the input_text to be changed.
        for match in matchedfields:
            inputtext_copy = inputtext_copy.replace(match, row[match]) ## this saves the text with the right 
        print inputtext ## should be the original w/ generic field names 
        print inputtext_copy ## should be the new version with all fields changed to specific instantiations

我认为真正的方法是使用字符串模板。让你的生活变得轻松

下面是一个在Python2和pytho3下工作的通用解决方案:

import string

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

import csv

with open('recipients.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(template_text.safe_substitute(row))
现在,我注意到您的csv有点被空格弄乱了,所以您必须首先处理好这一点(或者调整调用以适应csv阅读器或模板)。

非常好。从不知道模板字符串(在Python2.4中添加)
import csv

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