Python如何替换括号中的特定字符串?
我有一个字符串,我想相应地替换括号中的特定字符串。字符串不是我自己创建的,因此我无法控制字符串的格式。我有的是Python如何替换括号中的特定字符串?,python,regex,Python,Regex,我有一个字符串,我想相应地替换括号中的特定字符串。字符串不是我自己创建的,因此我无法控制字符串的格式。我有的是示例和价格,我想用定义的苹果价格替换[apple\u price]。这是一个例子 apple_price = 50 orange_price = 40 example_str = "Apple costs $ [apple_price] and Orange costs $ [orange_price]" Expected Output: "Apple c
示例和价格
,我想用定义的苹果价格
替换[apple\u price]
。这是一个例子
apple_price = 50
orange_price = 40
example_str = "Apple costs $ [apple_price] and Orange costs $ [orange_price]"
Expected Output: "Apple costs $ 50 and Orange costs $ 40"
我试图用re
替换它,但它将相同的字符串替换为所有括号
re.sub("[\[].*?[\)\]]", apple_price , example_str )
您可以使用格式化字符串
。格式化字符串的工作原理是将“{}
”括号添加到字符串中,然后使用format
函数按顺序添加变量。它们将添加到括号所在的位置
因此,在你的情况下:
apple_price = 50
orange_price = 40
str = "Apple costs $ {} and Orange costs $ {}"
print(str.format(apple_price, orange_price))
您可以使用格式化字符串
。格式化字符串的工作原理是将“{}
”括号添加到字符串中,然后使用format
函数按顺序添加变量。它们将添加到括号所在的位置
因此,在你的情况下:
apple_price = 50
orange_price = 40
str = "Apple costs $ {} and Orange costs $ {}"
print(str.format(apple_price, orange_price))
re.sub
接受可调用的替换,以动态构造替换字符串。可调用的参数是re.Match
对象
修改模式以捕获变量名。对于不存在的变量,不执行替换,即返回组(0)
重新导入
text=“苹果成本$[Apple\u price]和橙色成本$[Orange\u price]$[不存在]”
变量={
“苹果价格”:50,
“橙色价格”:40,
}
打印(关于(
r“[\[](.*?[\)\]]”,
lambda m:str(variables.get(m.group(1),m.group(0)),
文本,
))
输出:
Apple costs $ 50 and Orange costs $ 40 $ [does_not_exist]
Apple costs $ 50 and Orange costs $ 40
re.sub
接受可调用的替换,以动态构造替换字符串。可调用的参数是re.Match
对象
修改模式以捕获变量名。对于不存在的变量,不执行替换,即返回组(0)
重新导入
text=“苹果成本$[Apple\u price]和橙色成本$[Orange\u price]$[不存在]”
变量={
“苹果价格”:50,
“橙色价格”:40,
}
打印(关于(
r“[\[](.*?[\)\]]”,
lambda m:str(variables.get(m.group(1),m.group(0)),
文本,
))
输出:
Apple costs $ 50 and Orange costs $ 40 $ [does_not_exist]
Apple costs $ 50 and Orange costs $ 40
在这种情况下,还可以使用字符串替换方法和格式:
apple_price = 50
orange_price = 40
str = "Apple costs $[apple_price] and Orange costs $[orange_price]"
result = str.replace('[', '{').replace(']', '}').replace('apple_price','').replace('orange_price','')
print(result.format(apple_price, orange_price))
# Apple costs $50 and Orange costs $40
在这种情况下,还可以使用字符串替换方法和格式:
apple_price = 50
orange_price = 40
str = "Apple costs $[apple_price] and Orange costs $[orange_price]"
result = str.replace('[', '{').replace(']', '}').replace('apple_price','').replace('orange_price','')
print(result.format(apple_price, orange_price))
# Apple costs $50 and Orange costs $40
在Python中使用格式
apple_price = 50
orange_price = 40
example_str = f"Apple costs $ {apple_price} and Orange costs $ {orange_price}"
在Python中使用格式
apple_price = 50
orange_price = 40
example_str = f"Apple costs $ {apple_price} and Orange costs $ {orange_price}"
您试图使用
re
模块,似乎使事情过于复杂。在您的情况下,只需使用内置的str.replace
方法就足够了,除非我遗漏了一些要求
apple\u价格=50
橙色价格=40
示例\u str=“苹果成本为$[Apple\u price]和橙色成本为$[Orange\u price]”
替换=示例替换(“[apple\u price]”,str(apple\u price))。替换(“[orange\u price]”,str(orange\u price))
打印(已替换)
这将用它们各自的值替换所有出现的[apple\u price]
和[orange\u price]
,并给出以下输出:
苹果售价50美元,橙色售价40美元
您试图使用
re
模块,似乎使事情过于复杂。在您的情况下,只需使用内置的str.replace
方法就足够了,除非我遗漏了一些要求
apple\u价格=50
橙色价格=40
示例\u str=“苹果成本为$[Apple\u price]和橙色成本为$[Orange\u price]”
替换=示例替换(“[apple\u price]”,str(apple\u price))。替换(“[orange\u price]”,str(orange\u price))
打印(已替换)
这将用它们各自的值替换所有出现的[apple\u price]
和[orange\u price]
,并给出以下输出:
苹果售价50美元,橙色售价40美元
我建议不要使用python保留名称作为变量名。在本例中,您使用
str
作为变量名,该变量名是为python中的string
类型保留的
话虽如此,这里有一个简单的代码
输出:
Apple costs $ 50 and Orange costs $ 40 $ [does_not_exist]
Apple costs $ 50 and Orange costs $ 40
您所要做的就是将变量作为字符串传递。要了解更多信息,我建议不要使用python保留名称作为变量名。在本例中,您使用
str
作为变量名,该变量名是为python中的string
类型保留的
话虽如此,这里有一个简单的代码
输出:
Apple costs $ 50 and Orange costs $ 40 $ [does_not_exist]
Apple costs $ 50 and Orange costs $ 40
您所要做的就是将变量作为字符串传递。要了解更多信息您是在其他地方创建字符串还是只创建一个格式字符串?@Sayse我无法控制字符串的格式。我需要将[String]替换为整数。强烈建议不要使用内置的
str
作为名称。@AivarPaalberg感谢您的提醒。事实上,情况并非如此。举个例子。CheesersOmetimes“just example”代码可能会以实际代码结尾:-),这可能与此相关:“特殊情况不足以违反规则。”您是在其他地方创建字符串还是只创建格式字符串?@Sayse我无法控制字符串的格式。我需要将[String]替换为整数。强烈建议不要使用内置的str
作为名称。@AivarPaalberg感谢您的提醒。事实上,情况并非如此。举个例子。CheesersOmetimes“just-example”代码可能会以实际代码结尾:-),这可能与此相关:“特殊情况不足以违反规则。”抱歉,可能我的描述不清楚。我无法控制字符串的格式。啊,好的。请把这个加在问题上。对不起,也许我的描述不清楚。我无法控制字符串的格式。啊,好的。请在问题中加上这个。