如何根据Python中的字典替换大字符串的子字符串?

发布于 2024-12-09 07:13:28 字数 427 浏览 0 评论 0原文

我有一个长字符串(一个“模板”),其中包含 %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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫性小仙女 2024-12-16 07:13:28

最简单的解决方案是

import re
re.sub(r'%(.+?)%', lambda m: rep_dict[m.group(1)], YOUR_TEMPLATE)

不够快?有人说“不要使用正则表达式”,你就服从了吗?使用 Python 中的某些代码解析模板会更加复杂和缓慢(不要忘记,re 是用 C 编写的)。

The easiest solution is

import re
re.sub(r'%(.+?)%', lambda m: rep_dict[m.group(1)], YOUR_TEMPLATE)

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).

吐个泡泡 2024-12-16 07:13:28

这太棒了。我一直以没有时间学习RegEx为借口,
但始终尊重它。这篇文章为我提供了开始的必要条件。这是我的解决方案
不过,我发现群组通话在字典参数中混淆了:

retVal          = re.sub(r'%title', theTitle, template)
retVal          = re.sub(r'%([a-z]+?)+', \
                    lambda m: myDict.get(m.group(0)[1:], ''), retVal)

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:

retVal          = re.sub(r'%title', theTitle, template)
retVal          = re.sub(r'%([a-z]+?)+', \
                    lambda m: myDict.get(m.group(0)[1:], ''), retVal)

title was not in the dictionary, that is why I did it first. Requrements of the others in the team.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文