2016-12-21 6 views
-1

このネストされたdictからチーム名だけをプリントアウトする方法は不思議です。Pythonでネストされたdictのキーと値を表示する方法

{ 
    "leagueCaption": "Premier League 2016/17", 
    "matchday": 17, 
    "standing": [ 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/5/5c/Chelsea_crest.svg", 
     "goalDifference": 24, 
     "goals": 35, 
     "goalsAgainst": 11, 
     "playedGames": 17, 
     "points": 43, 
     "rank": 1, 
     "team": "Chelsea", 
     "teamId": 61 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/0/0a/FC_Liverpool.svg", 
     "goalDifference": 21, 
     "goals": 41, 
     "goalsAgainst": 20, 
     "playedGames": 17, 
     "points": 37, 
     "rank": 2, 
     "team": "Liverpool", 
     "teamId": 64 
    }, 
    { 
     "crestURI": "https://upload.wikimedia.org/wikipedia/en/e/eb/Manchester_City_FC_badge.svg", 
     "goalDifference": 16, 
     "goals": 36, 
     "goalsAgainst": 20, 
     "playedGames": 17, 
     "points": 36, 
     "rank": 3, 
     "team": "ManCity", 
     "teamId": 65 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/en/5/53/Arsenal_FC.svg", 
     "goalDifference": 19, 
     "goals": 38, 
     "goalsAgainst": 19, 
     "playedGames": 17, 
     "points": 34, 
     "rank": 4, 
     "team": "Arsenal", 
     "teamId": 57 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/b/b4/Tottenham_Hotspur.svg", 
     "goalDifference": 17, 
     "goals": 29, 
     "goalsAgainst": 12, 
     "playedGames": 17, 
     "points": 33, 
     "rank": 5, 
     "team": "Spurs", 
     "teamId": 73 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg", 
     "goalDifference": 7, 
     "goals": 24, 
     "goalsAgainst": 17, 
     "playedGames": 17, 
     "points": 30, 
     "rank": 6, 
     "team": "ManU", 
     "teamId": 66 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/c/c9/FC_Southampton.svg", 
     "goalDifference": 1, 
     "goals": 17, 
     "goalsAgainst": 16, 
     "playedGames": 17, 
     "points": 24, 
     "rank": 7, 
     "team": "Southampton", 
     "teamId": 340 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/8/8b/West_Bromwich_Albion.svg", 
     "goalDifference": 2, 
     "goals": 23, 
     "goalsAgainst": 21, 
     "playedGames": 17, 
     "points": 23, 
     "rank": 8, 
     "team": "West Bromwich", 
     "teamId": 74 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/f/f9/Everton_FC.svg", 
     "goalDifference": 0, 
     "goals": 21, 
     "goalsAgainst": 21, 
     "playedGames": 17, 
     "points": 23, 
     "rank": 9, 
     "team": "Everton", 
     "teamId": 62 
    }, 
    { 
     "crestURI": "https://upload.wikimedia.org/wikipedia/de/4/41/Afc_bournemouth.svg", 
     "goalDifference": -5, 
     "goals": 23, 
     "goalsAgainst": 28, 
     "playedGames": 17, 
     "points": 21, 
     "rank": 10, 
     "team": "Bournemouth", 
     "teamId": 1044 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/a/a3/Stoke_City.svg", 
     "goalDifference": -5, 
     "goals": 19, 
     "goalsAgainst": 24, 
     "playedGames": 17, 
     "points": 21, 
     "rank": 11, 
     "team": "Stoke", 
     "teamId": 70 
    }, 
    { 
     "crestURI": "https://upload.wikimedia.org/wikipedia/en/e/e2/Watford.svg", 
     "goalDifference": -8, 
     "goals": 21, 
     "goalsAgainst": 29, 
     "playedGames": 17, 
     "points": 21, 
     "rank": 12, 
     "team": "Watford", 
     "teamId": 346 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/e/e0/West_Ham_United_FC.svg", 
     "goalDifference": -12, 
     "goals": 19, 
     "goalsAgainst": 31, 
     "playedGames": 17, 
     "points": 19, 
     "rank": 13, 
     "team": "West Ham", 
     "teamId": 563 
    }, 
    { 
     "crestURI": "https://upload.wikimedia.org/wikipedia/en/2/2c/Middlesbrough_FC_crest.svg", 
     "goalDifference": -3, 
     "goals": 16, 
     "goalsAgainst": 19, 
     "playedGames": 17, 
     "points": 18, 
     "rank": 14, 
     "team": "Middlesbrough", 
     "teamId": 343 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/en/6/63/Leicester02.png", 
     "goalDifference": -6, 
     "goals": 23, 
     "goalsAgainst": 29, 
     "playedGames": 17, 
     "points": 17, 
     "rank": 15, 
     "team": "Foxes", 
     "teamId": 338 
    }, 
    { 
     "crestURI": "https://upload.wikimedia.org/wikipedia/en/0/02/Burnley_FC_badge.png", 
     "goalDifference": -12, 
     "goals": 16, 
     "goalsAgainst": 28, 
     "playedGames": 17, 
     "points": 17, 
     "rank": 16, 
     "team": "Burnley", 
     "teamId": 328 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/b/bf/Crystal_Palace_F.C._logo_%282013%29.png", 
     "goalDifference": -4, 
     "goals": 28, 
     "goalsAgainst": 32, 
     "playedGames": 17, 
     "points": 15, 
     "rank": 17, 
     "team": "Crystal", 
     "teamId": 354 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/6/60/AFC_Sunderland.svg", 
     "goalDifference": -13, 
     "goals": 15, 
     "goalsAgainst": 28, 
     "playedGames": 17, 
     "points": 14, 
     "rank": 18, 
     "team": "Sunderland", 
     "teamId": 71 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/a/ab/Swansea_City_Logo.svg", 
     "goalDifference": -17, 
     "goals": 20, 
     "goalsAgainst": 37, 
     "playedGames": 17, 
     "points": 12, 
     "rank": 19, 
     "team": "Swans", 
     "teamId": 72 
    }, 
    { 
     "crestURI": "http://upload.wikimedia.org/wikipedia/de/a/a9/Hull_City_AFC.svg", 
     "goalDifference": -22, 
     "goals": 14, 
     "goalsAgainst": 36, 
     "playedGames": 17, 
     "points": 12, 
     "rank": 20, 
     "team": "Hull", 
     "teamId": 322 
    } 
    ] 
} 

私はチームを印刷するだけです。この言葉をレスポンスといいますと、私はできると思いました:

team = response['standing']['team'] 

私はエラーが表示されません。

私はこれをフラスコで実行していますが、ここにはPythonコードがあります!

本質的に私はEPLリーグテーブルを印刷しようとしています!

from flask import Flask, jsonify, render_template, request 
import httplib 
import json 

app = Flask(__name__) 

@app.route('/') 
def index1(): 
    return render_template('league.html') 

@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': 'mytoken', 'X-Response-Control': 'minified'} 
    connection.request('GET', '/v1/competitions/'+text+'/leagueTable', None, headers) 
    response = json.loads(connection.getresponse().read().decode()) 
    return jsonify(response) 



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

'response ['standing'] [0] ['team']'はレスポンス['standing'] 'がリストであり辞書ではないので 'Chelsea'を生成します。 'response ['standing']'を繰り返し、各itemに対してitem ['team'] 'を使用することができます – sberry

答えて

0

teamnames = [r['team'] for r in response['standing'] ] 

これが返されます:[「チェルシー」、「リバプール「マンチェスター」、「アーセナル」、「スパーズ」、「マヌー」、「サザンプトン」、「ウェスト・ブロムウィッチ」、「エバートン」、「ボーンマス」、「ストーク」、「ワトフォード」、「ウェストハム」、「ミドルスブラ」 '、' Foxes '、' Burnley '、' Crystal '、' Sunderland '、' Swans '、' Hull ']

あなたがTEAMNAMEを望んでいた場合

points=[r['team']+": "+str(r['points']) for r in response['standing']] 

:あなたが使用できる辞書としてのポイント:

points={r['team']:r['points'] for r in response['standing']} 
+0

これはすばらしいです! –

+0

もし私がチームネームを返そうと思えば、ポイントはチェルシー:44など....私はそれをどうやってやるの? –

1

dict["standing"]list値を保持しているので、あなたは、キーとして"team"にアクセスすることはできません。すべてのチームを印刷するために、実行します。my_jsonはあなたのdictオブジェクトを保持

for standing in my_json['standing']: 
    print standing['team'] 

。あなたはすべてのチームのlistを取得したい場合は、あなたのようにリスト内包表現を使用することがあります。

1行で
[standing['team'] for standing in my_json['standing']] 
-1
team = response['standing'][0]['team'] 

[0]インデックスがアクセスするために使用される文字列のリストとしてのポイント:TEAMNAMEを戻します複数の辞書としてstandingの最初の辞書がstandingキーの中に存在します。

関連する問題