我想做一个代码,只替换字符串变量中的整个单词。首先我发现re.sub
解决方案,但没有提到关于变量里面。 现在我的解决方案看起来(它不会取代整个词):替换整个词Python(仅变量)
for index in (range(len(lista)):
query=query.replace(lista[index],var)
如上的例子,我想在lista[index]
与价值从VAR -> variable
替换值。
编辑:
例子查询:
Select Information,AdditionalInformation,Price from Table
例如LISTA瓦莱斯:
Information, Price
例如VAR:
var = "hide"
最后查询应该长相:
Select hide,AdditionalInformation,hide from Table
什么查询包含?你到底需要取代什么? – Harsha
[用python中的字符串替换另一个词的可能的重复](http://stackoverflow.com/questions/16600823/replacing-word-with-another-word-from-the-string-in-python) –
不相关,但你可以像这样简化你的块:'对于列表中的元素: query = query.replace(elem,var)' – Julien