【问题标题】:How to replace substrings like NAME in a string template?如何在字符串模板中替换像 NAME 这样的子字符串?
【发布时间】:2017-03-09 09:17:52
【问题描述】:

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

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

我有一个格式的 csv(第一行是标题)

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

我正在尝试遍历 csv 文件中的每一行,将 text 中的子字符串替换为与原始子字符串匹配的标题行的列中的 csv 元素。我有一个名为 matchedfields 的列表,其中包含在 csv 标题行和 text 中找到的所有字段(以防我不需要使用 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!"

【问题讨论】:

    标签: python replace substring template-strings


    【解决方案1】:

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

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

    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 阅读器或模板的调用)。

    【讨论】:

    【解决方案2】:

    问题在于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
    

    【讨论】:

      【解决方案3】:

      您应该将被替换的字符串重新分配给原始名称,这样之前的替换就不会被丢弃:

      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
      

      这一步用字典格式化字符串,无需反复进行替换。

      【讨论】:

        猜你喜欢
        • 2019-05-08
        • 2011-10-14
        • 1970-01-01
        • 1970-01-01
        • 2014-01-21
        • 1970-01-01
        • 2020-04-05
        • 2012-04-03
        • 2013-07-23
        相关资源
        最近更新 更多