Python 3:用多个相同的关键字格式化一个字符串,但用唯一的字符串来替换它们
Python 3: Formatting a string with multiple same keywords but unique strings to replace them
我想知道如何在 Python 3.4 中使用列表来格式化多个相同的关键字。我设置它的方式是用户可以传入一个字符串,该字符串具有多个关键字,程序将在它们的位置生成名称,结果应该是一个句子,关键字替换为名称。
我已经创建了一种方法,可以根据程序在用户传入的字符串中看到的数量来生成名称,但由于字符串的性质,无法立即替换它们。由于该字符串有多个相同的关键字(例如 {name}),我需要能够用一个唯一的字符串替换它们中的每一个。这在 Python 3.4 中可能吗?
用户传入的字符串可以是
"{name} had a wonderful day at the park, but then {name} came and ruined it"
程序生成名称后应该是
"John had a wonderful day at the park, but then Bob came and ruined it"
干杯。
编辑:要补充一点,我没有发现任何关于使用列表或具有多个关键字但唯一替换的内容,所以如果我必须用另一种方式而不是替换它也可以。
您可以将 string.replace
与可选的 count
参数一起使用,并将其限制为每次仅替换一个名称:
>>> names = ['John', 'Bob']
>>> message = "{name} had a wonderful day at the park, but then {name} came and ruined it"
>>> i = 0;
>>> while '{name}' in message:
... message = message.replace('{name}', names[i], 1)
... i += 1
...
>>> message
'John had a wonderful day at the park, but then Bob came and ruined it'
您可以使用count
参数:
>>> s = "{name} had a wonderful day at the park, but then {name} came and ruined it"
>>> s = s.replace('{name}', 'John', count=1)
>>> s
'John had a wonderful day at the park, but then {name} came and ruined it'
>>> s = s.replace('{name}', 'Bob', count=1)
>>> s
'John had a wonderful day at the park, but then Bob came and ruined it'
如果我没理解错的话,这应该有效:
first_name = Bob
second_name = Sam
"%s had a wonderful day at the park, but then %s came and ruined it" % (first_name, second_name)
可能是最简洁的方法
如果您已经根据每个替换的出现次数预先生成了一个项目列表,您可以使用 re.sub
以编程方式从列表中选择下一个项目。这将比 str.replace
具有更好的性能,尤其是。如果您有大量关键字词典和大量文本。
例如:
import re
# Function to look up an item and return the next thing in its list.
def replace(m):
return D[m.group(1)].pop(0)
D = {'name' : ['John','Bob'], 'place' : ['park']}
text = "{name} had a wonderful day at the {place}, but then {name} came and ruined it"
new_text = re.sub('{(.*?)}',replace,text)
print(new_text)
输出:
John had a wonderful day at the park, but then Bob came and ruined it
不过,您似乎想要不同名称的不同变量。然后你可以只使用 format
和字典:
重新导入
D = {'name1':'John', 'name2':'Bob', 'place':'park'}
text = "{name1} had a wonderful day at the {place}, but then {name2} came and ruined it. {name2} is a jerk!"
print(text.format(**D))
输出:
John had a wonderful day at the park, but then Bob came and ruined it. Bob is a jerk!
如果短语总是像示例中那样间隔良好,您可以这样做
s = "{name} had a wonderful day at the park, but then {name} came and ruined it"
names = ['John', 'Bob']
ni = iter(names)
outs = ' '.join(next(ni) if el=='{name}' else el for el in s.split())
print(outs)
产生
'John had a wonderful day at the park, but then Bob came and ruined it'
我想知道如何在 Python 3.4 中使用列表来格式化多个相同的关键字。我设置它的方式是用户可以传入一个字符串,该字符串具有多个关键字,程序将在它们的位置生成名称,结果应该是一个句子,关键字替换为名称。
我已经创建了一种方法,可以根据程序在用户传入的字符串中看到的数量来生成名称,但由于字符串的性质,无法立即替换它们。由于该字符串有多个相同的关键字(例如 {name}),我需要能够用一个唯一的字符串替换它们中的每一个。这在 Python 3.4 中可能吗?
用户传入的字符串可以是
"{name} had a wonderful day at the park, but then {name} came and ruined it"
程序生成名称后应该是
"John had a wonderful day at the park, but then Bob came and ruined it"
干杯。
编辑:要补充一点,我没有发现任何关于使用列表或具有多个关键字但唯一替换的内容,所以如果我必须用另一种方式而不是替换它也可以。
您可以将 string.replace
与可选的 count
参数一起使用,并将其限制为每次仅替换一个名称:
>>> names = ['John', 'Bob']
>>> message = "{name} had a wonderful day at the park, but then {name} came and ruined it"
>>> i = 0;
>>> while '{name}' in message:
... message = message.replace('{name}', names[i], 1)
... i += 1
...
>>> message
'John had a wonderful day at the park, but then Bob came and ruined it'
您可以使用count
参数:
>>> s = "{name} had a wonderful day at the park, but then {name} came and ruined it"
>>> s = s.replace('{name}', 'John', count=1)
>>> s
'John had a wonderful day at the park, but then {name} came and ruined it'
>>> s = s.replace('{name}', 'Bob', count=1)
>>> s
'John had a wonderful day at the park, but then Bob came and ruined it'
如果我没理解错的话,这应该有效:
first_name = Bob
second_name = Sam
"%s had a wonderful day at the park, but then %s came and ruined it" % (first_name, second_name)
可能是最简洁的方法
如果您已经根据每个替换的出现次数预先生成了一个项目列表,您可以使用 re.sub
以编程方式从列表中选择下一个项目。这将比 str.replace
具有更好的性能,尤其是。如果您有大量关键字词典和大量文本。
例如:
import re
# Function to look up an item and return the next thing in its list.
def replace(m):
return D[m.group(1)].pop(0)
D = {'name' : ['John','Bob'], 'place' : ['park']}
text = "{name} had a wonderful day at the {place}, but then {name} came and ruined it"
new_text = re.sub('{(.*?)}',replace,text)
print(new_text)
输出:
John had a wonderful day at the park, but then Bob came and ruined it
不过,您似乎想要不同名称的不同变量。然后你可以只使用 format
和字典:
重新导入
D = {'name1':'John', 'name2':'Bob', 'place':'park'}
text = "{name1} had a wonderful day at the {place}, but then {name2} came and ruined it. {name2} is a jerk!"
print(text.format(**D))
输出:
John had a wonderful day at the park, but then Bob came and ruined it. Bob is a jerk!
如果短语总是像示例中那样间隔良好,您可以这样做
s = "{name} had a wonderful day at the park, but then {name} came and ruined it"
names = ['John', 'Bob']
ni = iter(names)
outs = ' '.join(next(ni) if el=='{name}' else el for el in s.split())
print(outs)
产生
'John had a wonderful day at the park, but then Bob came and ruined it'