Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表作为flask应用程序的元素列返回_Python_Json_Python 2.7_Flask - Fatal编程技术网

Python 将列表作为flask应用程序的元素列返回

Python 将列表作为flask应用程序的元素列返回,python,json,python-2.7,flask,Python,Json,Python 2.7,Flask,我的文件file.json只有一个键“file”,它返回一个列表[1,2,3,4]。 我想在网页上返回此信息: 我的名单 一, 二, 三, 四, 但我得到的却是:我的列表[1,2,3,4] from flask import Flask, render_template import json app=Flask(__name__) @app.route('/') def hello(): return render_template('index.html') @app.rout

我的文件file.json只有一个键“file”,它返回一个列表[1,2,3,4]。 我想在网页上返回此信息:

我的名单

一,

二,

三,

四,

但我得到的却是:我的列表[1,2,3,4]

from flask import Flask, render_template
import json

app=Flask(__name__)

@app.route('/')
def hello():
    return render_template('index.html')

@app.route('/data/')
def file():
    with open('file.json') as data:
            Data = json.load(data)
    return "My List " + str(Data['file'])
尝试Python的无响应输出

要通过jinja进行渲染,可以使用如下过滤器:

{{ data|pprint }}
或者,您可以使用多种不同的策略手动使用换行符格式化输出:

@app.route('/data/')
def file():
    with open('file.json') as data:
            Data = json.load(data)
    return "My List \n\n" + '\n\n'.join([str(x) for x in Data['file']])
尝试Python的无响应输出

要通过jinja进行渲染,可以使用如下过滤器:

{{ data|pprint }}
或者,您可以使用多种不同的策略手动使用换行符格式化输出:

@app.route('/data/')
def file():
    with open('file.json') as data:
            Data = json.load(data)
    return "My List \n\n" + '\n\n'.join([str(x) for x in Data['file']])
这样写:

@app.route('/data/')
def file():
    with open('file.json') as data:
            Data = json.load(data)
    return 'My List <br><br>' + '<br>'.join(map(str, Data['file']))
@app.route(“/data/”)
def文件():
以open('file.json')作为数据:
Data=json.load(数据)
返回“我的列表”+“
”。加入(映射(str,数据['file']))
这样写:

@app.route('/data/')
def file():
    with open('file.json') as data:
            Data = json.load(data)
    return 'My List <br><br>' + '<br>'.join(map(str, Data['file']))
@app.route(“/data/”)
def文件():
以open('file.json')作为数据:
Data=json.load(数据)
返回“我的列表”+“
”。加入(映射(str,数据['file']))
必须使用循环将数字逐个添加到新行字符串中。必须使用循环将数字逐个添加到新行字符串中。