gpt4 book ai didi

python - 替换整个单词 Python(仅变量)

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:37 24 4
gpt4 key购买 nike

我正在尝试编写一个仅替换字符串变量中整个单词的代码。首先我找到了 re.sub 解决方案,但没有提到内部变量。现在我的解决方案看起来(它不会替换整个单词):

for index in (range(len(lista)):
query=query.replace(lista[index],var)

如上例所示,我想用 VAR -> variable 中的值替换 lista[index] 中的值。

编辑:

示例查询:

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 - 替换整个单词 Python(仅变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com