简体   繁体   English

在.replace() 中使用占位符

[英]Use placeholder inside .replace()

With Python I need to replace a text but parts of the text is dynamical created.使用 Python 我需要替换文本,但部分文本是动态创建的。 The start and finish of the string is always the same and unique.字符串的开头和结尾始终相同且唯一。

Can I use placeholders inside filedata.replace() or is there a different method to solve that problem?我可以在filedata.replace()中使用占位符吗?还是有其他方法可以解决该问题?

This did not work:这没有用:

filedata = filedata.replace('<a href="/link/%s">Link</a>', '<a href="/link/replaced">Link</a>')

Solution based on the comments under my question:基于我的问题下的评论的解决方案:

import re
filedata = re.sub('<a href="/link/(.*?)">Link</a>', '<a href="/link/replaced">Link</a>', filedata)

Read about regular expressions .阅读有关正则表达式的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM