Python 在jinja中将字符串拆分为列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30515456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string into list in jinja?
提问by user3605780
I have some variables in a jinja2 template which are strings seperated by a ';'.
我在 jinja2 模板中有一些变量,它们是由“;”分隔的字符串。
I need to use these strings separately in the code. i.e. the variable is variable1 = "green;blue"
我需要在代码中单独使用这些字符串。即变量是 variable1 = "green;blue"
{% list1 = {{ variable1 }}.split(';') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}
I can split them up before rendering the template but since it are sometimes up to 10 strings inside the string this gets messy.
我可以在渲染模板之前将它们分开,但由于有时字符串中最多有 10 个字符串,这会变得混乱。
I had a jsp before where I did:
我之前有一个jsp:
<% String[] list1 = val.get("variable1").split(";");%>
The grass is <%= list1[0] %> and the boat is <%= list1[1] %>
EDIT:
编辑:
It works with:
它适用于:
{% set list1 = variable1.split(';') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}
采纳答案by user3605780
After coming back to my own question after 5 year and seeing so many people found this useful, a little update.
在 5 年后回到我自己的问题并看到这么多人发现这很有用,一点点更新。
A string variable can be split into a list
by using the split function (it can contain similar values, set
is for the assignment) . I haven't found this function in the official documentation but it works similar to normal Python. The items can be called via an index, used in a loop or like Dave suggested if you know the values, it can set variables like a tuple.
可以list
使用 split 函数将字符串变量拆分为一个(它可以包含相似的值,set
用于赋值)。我在官方文档中没有找到这个函数,但它的工作原理类似于普通的 Python。这些项目可以通过索引调用,在循环中使用,或者像戴夫建议的那样,如果你知道这些值,它可以像元组一样设置变量。
{% set list1 = variable1.split(';') %}
The grass is {{ list1[0] }} and the boat is {{ list1[1] }}
or
或者
{% set list1 = variable1.split(';') %}
{% for item in list1 %}
<p>{{ item }}<p/>
{% endfor %}
or
或者
{% set item1, item2 = variable1.split(';') %}
The grass is {{ item1 }} and the boat is {{ item2 }}
回答by poke
You can't run arbitrary Python code in jinja; it doesn't work like JSP in that regard (it just looks similar). All the things in jinja are custom syntax.
你不能在 jinja 中运行任意的 Python 代码;在这方面它不像 JSP 那样工作(它看起来很相似)。jinja 中的所有东西都是自定义语法。
For your purpose, it would make most sense to define a custom filter, so you could for example do the following:
为了您的目的,定义自定义过滤器最有意义,因此您可以执行以下操作:
The grass is {{ variable1 | splitpart(0, ',') }} and the boat is {{ splitpart(1, ',') }}
Or just:
The grass is {{ variable1 | splitpart(0) }} and the boat is {{ splitpart(1) }}
The filter function could then look like this:
过滤器函数可能如下所示:
def splitpart (value, index, char = ','):
return value.split(char)[index]
An alternative, which might make even more sense, would be to split it in the controller and pass the splitted list to the view.
另一种可能更有意义的替代方法是在控制器中拆分它并将拆分后的列表传递给视图。
回答by Waqar Detho
If there are up to 10 strings then you should use a list in order to iterate through all values.
如果最多有 10 个字符串,那么您应该使用一个列表来遍历所有值。
{% set list1 = variable1.split(';') %}
{% for list in list1 %}
<p>{{ list }}</p>
{% endfor %}