在这段代码中使用列表中的值时遇到问题。Return语法打破了循环,我可以使用列表中的所有数据。这是我的代码:
@app.route('/data', methods=["GET", "POST"])
def data():
value1 = [1, 2, 3, 4, 5, 6]
for i in range(0,len(value1)):
data = [time() * 1000, value1[i]]
response = make_response(json.dumps(data))
response.content_type = 'application/json'
return response
感谢
尝试声明一个空列表,并在返回之前将结果附加到for循环中的列表中,如下所示:
@app.route('/data', methods=["GET", "POST"])
def data():
value1 = [1, 2, 3, 4, 5, 6]
response_arr = []
for i in range(0,len(value1)):
data = [time() * 1000, value1[i]]
response = make_response(json.dumps(data))
response.content_type = 'application/json'
response_arr.append(response)
return response_arr
试试这样的东西:
def data():
all_data = []
value1 = [1, 2, 3, 4, 5, 6]
for number,value in enumerate(value1):
data = [time() * 1000, value]
all_data.append(data)
response = make_response(json.dumps(all_data))
response.content_type = 'application/json'
return response