如何根据Python中的字典替换大字符串的子字符串?
我有一个长字符串(一个“模板”),其中包含 %MARK% 形式的“替换点”(单个给定标记的字符串中也可能出现更多次)。我想替换这些由 Python 字典控制的标记(它不包含标记的 % 符号),例如:
rep_dict = { "TITLE": "This is my title", "CONTENT": "Here it is the content" }
问题:简单地一一调用 replace()
方法并不好解决办法:之前的替换可能包含这些标记之一,那么就不能替换!
该解决方案应该足够快,因为我有很大的模板,并且我需要在一个大循环中替换其中的许多模板。我有一个非常丑陋且冗长的实现,其中有许多 find()
's,在替换过程中计算原始字符串中的偏移量等。我希望有一个更好、更紧凑的、和更快的解决方案。
I have a long string (a "template") containing "replacement points" in the form of %MARK% (there can be more occurences in the string for a single given marker too). I want to replace these markers, controlled by a Python dictionary (it does not contain the % signs for markers), like:
rep_dict = { "TITLE": "This is my title", "CONTENT": "Here it is the content" }
The problem: simple call of replace()
method one by one is not a good solution: the previous replacement may contain one of these marks, which then must not be replaced!
The solution should be fast enough, since I have large templates, and I need to replace many of them within a big loop. I have a very ugly and long implementation with many find()
's, counting offsets in the original string during the replacament process, etc. I have the hope that there is a much nicer, more compact, and quicker solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的解决方案是
不够快?有人说“不要使用正则表达式”,你就服从了吗?使用 Python 中的某些代码解析模板会更加复杂和缓慢(不要忘记,
re
是用 C 编写的)。The easiest solution is
Not fast enough? Someone said 'do not use regex' and you obey? Parsing your template using some code in Python would be even more complex and slow (don't forget,
re
is written in C).这太棒了。我一直以没有时间学习RegEx为借口,
但始终尊重它。这篇文章为我提供了开始的必要条件。这是我的解决方案
不过,我发现群组通话在字典参数中混淆了:
title
不在字典中,这就是我首先这样做的原因。团队中其他人的要求。This was excellent. I have always used the excuse of not having time to learn RegEx,
but always respected it. This post gave me the necessary to get started. This was my solution
though, I found the group call was mixed up in the dictionary parameters:
title
was not in the dictionary, that is why I did it first. Requrements of the others in the team.