在Python的Jinja模板中,如何将整数转换为序数字符串

huangapple go评论99阅读模式
英文:

How to convert a integer to ordinal string in python jinja templates

问题

I want to replace 1 with 1st, 2 with 2nd, and so on, in the jinja template.

Currently, I'm using the following code, but it is throwing SyntaxError

{{ "%d%s"% (no_of_owners, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}

where no_of_owners is a string like "5".
What should be the correct way to achieve this result?

英文:

I want to replace 1 with 1st, 2 with 2nd, and so on, in the jinja template.

Currently, I'm using the following code, but it is throwing SyntaxError

{{ &quot;%d%s&quot;% (no_of_owners, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}

where no_of_owners is a string like "5".
What should be the correct way to achieve this result?

答案1

得分: 0

错误看起来很明显:

TypeError: %d 格式需要实数而不是字符串

你正在尝试使用 %d 格式化一个字符串,这是没有意义的。你可以简单地将其替换为 %s

&gt;&gt;&gt; t = jinja2.Template('' '{{ "%s%s"% (no_of_owners, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}' '')
&gt;&gt;&gt; print(t.render(no_of_owners="5"))
5th

或者添加 int 过滤器:

&gt;&gt;&gt; t = jinja2.Template('' '{{ "%d%s"% (no_of_owners|int, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}' '')
&gt;&gt;&gt; print(t.render(no_of_owners="5"))
5th

更新 1

以下是上述示例的可运行版本:

import jinja2

print("=== 使用 %s 替代 %d ===")
t = jinja2.Template('' '{{ "%s%s"% (no_of_owners, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}' '')
print(t.render(no_of_owners="5"))
print()

print("=== 使用 int 过滤器 ===")
t = jinja2.Template('' '{{ "%d%s"% (no_of_owners|int, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}' '')
print(t.render(no_of_owners="5"))
print()

我在 Jinja2 3.0.3 和 Python 3.11 中进行了测试。

更新 2

上述代码也适用于 Python 3.6 和 Jinja2 2.10.1:

$ python
Python 3.6.15 (default, Dec 21 2021, 12:03:22)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import jinja2
&gt;&gt;&gt; jinja2.__version__
'2.10.1'
&gt;&gt;&gt; t = jinja2.Template('' '{{ "%s%s"% (no_of_owners, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}' '')
&gt;&gt;&gt; print(t.render(no_of_owners="5"))
5th
&gt;&gt;&gt; t = jinja2.Template('' '{{ "%d%s"% (no_of_owners|int, "tsnrhtdd"[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10<4) * no_of_owners|int%10 :: 4]) }}' '')
&gt;&gt;&gt; print(t.render(no_of_owners="5"))
5th
英文:

The error seems pretty clear:

TypeError: %d format: a real number is required, not str

You're trying to format a string using %d, which doesn't make any sense. You could simply replace it with %s:

&gt;&gt;&gt; t = jinja2.Template(&#39;&#39;&#39;{{ &quot;%s%s&quot;% (no_of_owners, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}&#39;&#39;&#39;)
&gt;&gt;&gt; print(t.render(no_of_owners=&quot;5&quot;))
5th

Or add the int filter:

&gt;&gt;&gt; t = jinja2.Template(&#39;&#39;&#39;{{ &quot;%d%s&quot;% (no_of_owners|int, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}&#39;&#39;&#39;)
&gt;&gt;&gt; print(t.render(no_of_owners=&quot;5&quot;))
5th

Update 1

Here's a runnable version of the above examples:

import jinja2

print(&quot;=== using %s instead of %d ===&quot;)
t = jinja2.Template(&#39;&#39;&#39;{{ &quot;%s%s&quot;% (no_of_owners, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}&#39;&#39;&#39;)
print(t.render(no_of_owners=&quot;5&quot;))
print()

print(&quot;=== using int filter ===&quot;)
t = jinja2.Template(&#39;&#39;&#39;{{ &quot;%d%s&quot;% (no_of_owners|int, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}&#39;&#39;&#39;)
print(t.render(no_of_owners=&quot;5&quot;))
print()

I'm testing this using Jinja2 3.0.3 and Python 3.11.

Update 2

The above code also works with Python 3.6 and Jinja2 2.10.1:

$ python
Python 3.6.15 (default, Dec 21 2021, 12:03:22)
[GCC 10.2.1 20210110] on linux
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import jinja2
&gt;&gt;&gt; jinja2.__version__
&#39;2.10.1&#39;
&gt;&gt;&gt; t = jinja2.Template(&#39;&#39;&#39;{{ &quot;%s%s&quot;% (no_of_owners, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}&#39;&#39;&#39;)
&gt;&gt;&gt; print(t.render(no_of_owners=&quot;5&quot;))
5th
&gt;&gt;&gt; t = jinja2.Template(&#39;&#39;&#39;{{ &quot;%d%s&quot;% (no_of_owners|int, &quot;tsnrhtdd&quot;[(no_of_owners|int//10%10!=1) * (no_of_owners|int%10&lt;4) * no_of_owners|int%10 :: 4]) }}&#39;&#39;&#39;)
&gt;&gt;&gt; print(t.render(no_of_owners=&quot;5&quot;))
5th

huangapple
  • 本文由 发表于 2023年7月24日 20:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定