Leo*_* He 3 c++ python jinja2 systemc
我的 Jinja2 模板中有这一行:
{% for type in types %}
top -> {{(loop.index0 + 'a')|string}}(var{{loop.index0}});
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
其中 types 是模板中其他地方使用的 C++ 中各种类型的列表,输出是用于初始化模块信号的 SystemC 程序的一部分。目标是获得这样的输出,其中字符以小写 a 开头:
top -> a(var0);
top -> b(var1);
Run Code Online (Sandbox Code Playgroud)
然而,它给出了这个错误:“+不支持的操作数类型:'int'和'str'”所以我尝试将模板更改为:
{% for type in types %}
top -> {{(loop.index0 + 'a'|int)|string}}(var{{loop.index0}});
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但随后的输出是这样的
top -> 0(var0);
top -> 1(var1);
Run Code Online (Sandbox Code Playgroud)
问题似乎在于 Jinja2 模板中无法将整数转换为相应的 ASCII 字符。我尝试了“chr()”,但这是一个 Python 函数,而不是 Jinja2 函数,并且不起作用。我想知道是否有人有这方面的经验并且可以帮助我?
回答标题问题提出的问题:“Jinja2 ASCII to String”
# Returns "a" if 'my_index' is 0, "b" if 1, etc.
{{ "abcdefghijklmnopqrstuvwxyz"[my_index] }}
# Use the char_code as the index.
{{ "abcdefghijklmnopqrstuvwxyz"[char_code - 97] }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3211 次 |
| 最近记录: |