これは非常に初心者の質問です。私は現在、特定の辞書値を取得中 - Python
import googlemaps
import json as js
from datetime import datetime
gmaps = googlemaps.Client(key=googleapikey)
my_distance = gmaps.distance_matrix('350 5th Ave, New York, NY 10118',
'4 Pennsylvania Plaza, New York, NY 10001')
print(my_distance)
プリントアウトは私が最終的に結果0.9キロを抽出したい次
{'status': 'OK', 'rows': [{'elements': [{'distance': {'value': 876, 'text': '0.9 km'}, 'duration': {'value': 324, 'text': '5 mins'}, 'status': 'OK'}]}], 'destination_addresses': ['4 Pennsylvania Plaza, New York, NY 10001, USA'], 'origin_addresses': ['350 5th Ave, New York, NY 10118, USA']}
で、次のコードを持っています。それ、どうやったら出来るの?
私は、エラー任意のヘルプは非常に感謝をいただければ幸いです
を取得し、私はTypeError: list indices must be integers or slices, not str
print(my_distance['rows'])
を使用してみましたが、それは私だけ
[{'elements': [{'status': 'OK', 'distance': {'value': 876, 'text': '0.9 km'}, 'duration': {'value': 324, 'text': '5 mins'}}]}]
print(my_distance['rows']['elements']['distance'])
を与えます!
_rows_と_elements_はリストなので、興味のあるレコードのインデックスを指定する必要があります: 'print(my_distance ['rows'] [0] ['elements'] [0] ['distance']) ' – agg3l
'print(my_distance [' rows '] [0] [' elements '] [0] [' distance '] [' text '])' –
それは多くの感謝の感謝をすることがわかります! – drsalt