替换整个单词 Python(仅变量) [英] Replace whole word Python (only variable)
本文介绍了替换整个单词 Python(仅变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个仅替换字符串变量中的整个单词的代码.首先,我找到了 re.sub
解决方案,但没有提到内部变量.现在我的解决方案看起来(它不会替换整个单词):
I am trying to do a code which replace only whole word in string variable.First of all I found re.sub
solution but there was no mentioned about variable inside.
Now my solution looks (it does not replace whole word):
for index in (range(len(lista)):
query=query.replace(lista[index],var)
如上例所示,我想用 VAR -> 中的值替换
.lista[index]
中的值变量
As on example above, I want to replace value in lista[index]
with value from VAR -> variable
.
示例查询:
Select Information,AdditionalInformation,Price from Table
示例列表:
Information, Price
示例变量:
var = "hide"
最后查询应该看起来:
Select hide,AdditionalInformation,hide from Table
推荐答案
我认为这应该可行:
for word in alist:
query = re.sub(r'\b'+word+r'\b',var,query)
这篇关于替换整个单词 Python(仅变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文