Python 将列表作为flask应用程序的元素列返回
我的文件file.json只有一个键“file”,它返回一个列表[1,2,3,4]。 我想在网页上返回此信息: 我的名单 一, 二, 三, 四, 但我得到的却是:我的列表[1,2,3,4]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
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']))
必须使用循环将数字逐个添加到新行字符串中。必须使用循环将数字逐个添加到新行字符串中。