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.
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,
reis written in C).