Flask和Python返回一个压缩列表

时间:2016-12-21 20:07:13

标签: python html flask

这令人非常沮丧,而且我一直在试图找到这个网站。

我的问题是我无法打印我的两个列表 - 团队名称和积分 - 彼此相邻。

当我打印(参见代码)时,它会正确打印到终端输出。

当我返回完全相同的东西时,我只是获得最后一个位置的队伍!

@app.route('/League Standing', methods=['GET','POST'])
    def show_league():
        text = request.form['league']
        connection = httplib.HTTPConnection('api.football-data.org')
        headers = {'X-Auth-Token': 'key', 'X-Response-Control': 'minified'}
        connection.request('GET', '/v1/competitions/'+text+'/leagueTable', None, headers)
        response = json.loads(connection.getresponse().read().decode())
        teamnames = [r['team'] for r in response['standing']]
        points = [r['points'] for r in response['standing']]
        for t, p in zip(teamnames, points):
           print('{}: {}'.format(t,p))
        return jsonify('{}: {}'.format(t,p))




    if __name__ == '__main__':
        app.run(debug=True)

以下是我打印到终端时会发生的情况:

Chelsea: 43
Liverpool: 37
ManCity: 36
Arsenal: 34
Spurs: 33
ManU: 30
Southampton: 24
West Bromwich: 23
Everton: 23
Bournemouth: 21
Stoke: 21
Watford: 21
West Ham: 19
Middlesbrough: 18
Foxes: 17
Burnley: 17
Crystal: 15
Sunderland: 14
Swans: 12
Hull: 12

当我打印到烧瓶网站时:

Hull: 12

我想将正在打印的内容返回到终端,网站!!!

请让我知道任何想法!

请帮忙。

1 个答案:

答案 0 :(得分:0)

您将完全按照所见内容返回:

jsonify('{}: {}'.format(t,p))

这将创建仅包含一个项目的字典(键t和值p,其值从之前的for循环中遗留下来。)

我建议首先通过循环压缩列表来创建字典,然后将该字典发送到jsonify。类似的东西:

return_dict = {}
for t, p in zip(teamnames, points):
    return_dict[t] = p
return jsonify(return_dict)