我在Jinja2中创建了一个发送到Flask中的函数的列表但是当我尝试使用str.join(' /',this_list)时,此函数返回一个单词列表,其中单词除以&# 39 /'
Jinja2片段:
{% set list1 = request.path[1:].split('/') %}
<a href="/activity/delete/{{ user_id }}/{{ activity.activity_id}}/{{ list1 }}">Delete</a>
返回
['activity', 'details', '98353a241d934856991cc11c5219bc4e', '8a30f573235f4f2c9cac2b516e5bfe42']
使用str.join(&#39; /&#39;,returned_value)我明白了:
[/'/a/c/t/i/v/i/t/y/'/,/ /'/d/e/t/a/i/l/s/'/,/ /'/9/8/3/5/3/a/2/4/1/d/9/3/4/8/5/6/9/9/1/c/c/1/1/c/5/2/1/9/b/c/4/e/'/,/ /'/8/a/3/0/f/5/7/3/2/3/5/f/4/f/2/c/9/c/a/c/2/b/5/1/6/e/5/b/f/e/4/2/'/]
我想得到:
活性/信息/ 98353a241d934856991cc11c5219bc4e / 8a30f573235f4f2c9cac2b516e5bfe42
怎么做?
答案 0 :(得分:0)
您需要将string
转换为list
import ast
returned_value = ast.literal_eval(returned_value)
"/".join(returned_value) #str.join('/', returned_value) also works.
答案 1 :(得分:-1)
尝试'/'.join()
或您的列表出现问题:
>>> list = ['activity', 'details', '98353a241d934856991cc11c5219bc4e', '8a30f573235f4f2c9cac2b516e5bfe42']
>>> '/'.join(list)
'activity/details/98353a241d934856991cc11c5219bc4e/8a30f573235f4f2c9cac2b516e5bfe42'
>>> str.join('/', list)
'activity/details/98353a241d934856991cc11c5219bc4e/8a30f573235f4f2c9cac2b516e5bfe42'