带有自定义占位符的高级 Python 字符串格式? [英] Advanced Python string formatting with custom placeholders?
问题描述
我在 Python 变量中有以下 HTML 模板:
I have the following HTML template in a Python variable:
template = """
<script>
function work()
{
alert('bla');
}
</script>
Success rate is {value:.2%} percent.
"""
我想用一些具有适当格式的十进制数替换 {value:.2%}
.由于我的模板可能包含大量 JavaScript 代码,我想避免使用 {{
和 }}
转义大括号,因此使用 template.format(...)
直接不是一个选项.
I want to substitute {value:.2%}
with some decimal number with the appropriate formatting. Since my template may contain a lot of JavaScript code, I want to avoid escaping the curly braces with {{
and }}
, so using template.format(...)
directly is not an option.
使用 Template(template).substitute(...)
似乎也不可能,因为我想要 value
的高级格式.
Using Template(template).substitute(...)
also seems impossible because I want advanced formatting for value
.
我可能可以用相应的 %(value)...
语法替换 {value:.2%}
,然后使用 template % ...代码>.但我不喜欢这种语法,因为现实世界模板中的大多数变量不需要高级格式,所以我想保持占位符语法简单.
I could probably replace {value:.2%}
with a corresponding %(value)...
syntax and then use template % ...
. But I'm not a fan of this syntax because most of the variables in a real-world template won't need advanced formatting, so I want to keep the placeholder syntax simple.
所以我的问题是,是否可以在模板中使用自定义占位符,以便在必要时进行高级格式设置,即仅使用 {{value}}
或 [[value]]
还有 {{value:.2%}}
或 [[value:.2%]]
?
So my question is, is it possible to use custom placeholders in the template that would allow for advanced formatting when necessary, i.e. to have simply {{value}}
or [[value]]
but also {{value:.2%}}
or [[value:.2%]]
?
最后,我想避免当模板包含占位符时 .format(...)
会产生的错误,例如 {{dont_want_this_located}}
,传递的字典不包含值.也就是说,我只想替换某些占位符,而不是模板中出现的所有占位符.
Finally, I want to avoid errors that .format(...)
would produce when the template contains placeholders, such as {{dont_want_this_substituted}}
, for which the passed dictionary doesn't contain a value. That is, I want to only substitute only certain placeholders, not all that appear in the template.
为了实现我想要的,我可以先用正则表达式获取所有占位符,然后格式化它们的内容,最后在模板中进行替换.但我想知道是否存在更简单的解决方案.
Edit 2: To achieve what I want, I can grab all placeholders with a regular expression first, then format their contents and finally make the replacement in the template. But I wonder if an easier solution exists.
编辑 3: 有人建议我拆分模板以避免 format()
出现问题.然而,我想避免这种情况,因为模板实际上来自一个文件.
Edit 3: It was suggested that I split the template to avoid issues with format()
. I would like to avoid this, however, because the template actually comes from a file.
推荐答案
只需预处理您的模板.例如,如果您希望 [[...]]
成为您的模板标记并保留单个 {
和 }
字符:>
Simply preprocess your template. For example, if you want [[...]]
to be your template markers and leave single {
and }
characters alone:
template = """
<script>
function work()
{
alert('bla');
}
</script>
Success rate is [[value:.2%]] percent.
"""
result = template.replace("{", "{{").replace("}", "}}").replace("[[", "{").replace("]]", "}").format(value=0.8675309)
试图用不同数量的大括号来完成这一切是很棘手的,所以我肯定会使用其他一些字符.但是请注意,像 [[
和 ]]
之类的东西可能会合理地出现在合法的 JavaScript 代码中.使用永远无法使用的东西可能会更好.
Trying to do it all with varying numbers of curly brackets is tricksy, so I would definitely use some other characters. Careful, though, things like [[
and ]]
could reasonably occur in legitimate JavaScript code. Might be better to use something that never could.
这篇关于带有自定义占位符的高级 Python 字符串格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!