2016-09-28 7 views
-2

私は、サーバーからバスタイムを取得してプリントするPythonでプログラムを作っています。PythonでJSONファイルのさまざまな部分を解析する方法は?

File "bus.py", line 10, in <module> 
    print info["route"] 
KeyError: 'route' 
を:私は duetimeと同じことをしようとすると、それが返され、ただし

info = json.load(req) 
print info["numberofresults"] 

を用いて、第1ブロックなどnumberofresultsを返す問題がない

{ 
"errorcode": "0", 
"errormessage": "", 
"numberofresults": 4, 
"stopid": "175", 
"timestamp": "28/09/2016 10:32:44", 
"results": [ 
{ 
"arrivaldatetime": "28/09/2016 10:43:36", 
"duetime": "10", 
"departuredatetime": "28/09/2016 10:43:36", 
"departureduetime": "10", 
"scheduledarrivaldatetime": "28/09/2016 10:44:00", 
"scheduleddeparturedatetime": "28/09/2016 10:44:00", 
"destination": "Kimmage", 
"destinationlocalized": "Camaigh", 
"origin": "Harristown", 
"originlocalized": "Baile Anraí", 
"direction": "Outbound", 
"operator": "bac", 
"additionalinformation": "", 
"lowfloorstatus": "no", 
"route": "83", 
"sourcetimestamp": "28/09/2016 09:44:49", 
"monitored": "true" 
}, 
{ 
"arrivaldatetime": "28/09/2016 10:43:56", 
"duetime": "11", 
"departuredatetime": "28/09/2016 10:43:56", 
"departureduetime": "11", 
"scheduledarrivaldatetime": "28/09/2016 10:14:00", 
"scheduleddeparturedatetime": "28/09/2016 10:14:00", 
"destination": "Kimmage", 
"destinationlocalized": "Camaigh", 
"origin": "Harristown", 
"originlocalized": "Baile Anraí", 
"direction": "Outbound", 
"operator": "bac", 
"additionalinformation": "", 
"lowfloorstatus": "no", 
"route": "83", 
"sourcetimestamp": "28/09/2016 10:32:40", 
"monitored": "true" 
}, { 
"errorcode": "0", 
"errormessage": "", 
"numberofresults": 4, 
"stopid": "175", 
"timestamp": "28/09/2016 10:32:44", 
"results": [ 
{ 
"arrivaldatetime": "28/09/2016 10:43:36", 
"duetime": "10", 
"departuredatetime": "28/09/2016 10:43:36", 
"departureduetime": "10", 
"scheduledarrivaldatetime": "28/09/2016 10:44:00", 
"scheduleddeparturedatetime": "28/09/2016 10:44:00", 
"destination": "Kimmage", 
"destinationlocalized": "Camaigh", 
"origin": "Harristown", 
"originlocalized": "Baile Anraí", 
"direction": "Outbound", 
"operator": "bac", 
"additionalinformation": "", 
"lowfloorstatus": "no", 
"route": "83", 
"sourcetimestamp": "28/09/2016 09:44:49", 
"monitored": "true" 
}, 
{ 
"arrivaldatetime": "28/09/2016 10:43:56", 
"duetime": "11", 
"departuredatetime": "28/09/2016 10:43:56", 
"departureduetime": "11", 
"scheduledarrivaldatetime": "28/09/2016 10:14:00", 
"scheduleddeparturedatetime": "28/09/2016 10:14:00", 
"destination": "Kimmage", 
"destinationlocalized": "Camaigh", 
"origin": "Harristown", 
"originlocalized": "Baile Anraí", 
"direction": "Outbound", 
"operator": "bac", 
"additionalinformation": "", 
"lowfloorstatus": "no", 
"route": "83", 
"sourcetimestamp": "28/09/2016 10:32:40", 
"monitored": "true" 
}, 

:JSONファイルには、次のようになります

これは、JSONファイル内の異なるバスで同じキーが複数回表示されるためです。どのバスを使ってPythonで情報を取得したいのですか?

ファイル "bus.py"、10行目、 印刷情報に[ '結果'] [ 'ルート'] 例外TypeError:それは働いて得ることができた:リストインデックスは整数でなければなりませんが、

EDITをstrがありませんprint info["results"][0]["route"]などを使用してください。ありがとうございます。

+1

いいえ、そのレベルにはその名前のキーがないためです。 'results'の要素に順番にアクセスします。 –

+0

'results'は複数の項目のリストです。それらを個別に反復処理してから、必要なバスを検索する必要があります。 –

+0

どのように私は探しているのですか? JSONは 'info = json.load(req)'としてロードされます –

答えて

0
print info['results']['routes'] 

上記を試してください。

+0

'File" bus.py "、行10、 印刷情報['results'] ['routes'] TypeError:リストインデックスはstrではない整数でなければなりません。 –

+0

問題ありますか?私の以前のコード? –

+0

私はそれを働かせることができました。あなたの答えは正しいです、私は 'print info [" results "] [0] [" route "]を追加しなければなりませんでした。ありがとうございました。 –

関連する問題