How to extract a specific value from a dictionary in python

2024/10/14 23:14:53

I want to extract distance from google distance matrix API in Python. The objet it returned is a python dictionary.

{'destination_addresses': [Mumbai, Maharashtra, India '],'origin_addresses': ['Powai, Mumbai, Maharashtra, India'],'rows': [{'elements': [{'distance': {'text': u '47.1 km','value': 47086},'duration': {'text': '1 hour 17 mins','value': 4628},'status': 'OK'}]}],'status': 'OK'}

I want to extract the distance and duration from above. I have used

distance = my_distance["rows"]
for a in distance:result = a["elements"]print result

It gave me

[{u 'duration': {u 'text': u '1 hour 17 mins', u 'value': 4628}, u 'distance': {u 'text': u '47.1 km', u 'value': 47086}, u 'status': u 'OK'
}]

Now how to extract duration (1 hour 17 mins) and distance (47.1 km) and their corresponding values (4628 & 47086)

Answer

You may simply do it as:

print "time taken : ", d["rows"][0]["elements"][0]["duration"]["text"]
print "distance : ", d["rows"][0]["elements"][0]["distance"]["text"]
print "distance value : ", d["rows"][0]["elements"][0]["distance"]["value"]
print "duration value : ", d["rows"][0]["elements"][0]["duration"]["value"]
https://en.xdnf.cn/q/117899.html

Related Q&A

label on top of image in python

I am trying to display text on top of an image. Right now the text is below the image or if I put a row=0 in the grid disappears. I am assuming it is behind the image. I cant seem to get it to work. My…

plot multiple graphs from multiple files gnuplot

I have a set of files named like this:qd-dPZ-z1-1nn.dat qd-dPZ-z2-1nn.dat qd-dPZ-z4-1nn.dat qd-dPZ-z8-1nn.dat qd-dPZ-z16-1nn.dat qd-dPZ-z32-1nn.dat qd-dPZ-z1-2nn.dat qd-dPZ-z2-2nn.dat qd-dPZ-z4…

Python writing to CSV... TypeError: coercing to Unicode: need string or buffer, file found

outputList is a list of lists. [ [a,b,c], [d,e,f], [g,h,i] ] and I want to output it to a csv file with each list as a separate row. Im getting this error TypeError: coercing to Unicode: need string or…

Preserve Signature in Decorator python 2

I am writing a decorator which will catch TypeError for incorrect number of arguments in a function call and will print a customised message. The code is here:import inspectdef inspect_signature(f):def…

Gimp: start script without image

Well, Im trying to write a python plug-in for Gimp, but it wont start without first loading an image... What can I do about that?

Pywinauto: how the `findbestmatch` module works?

Im trying to understand how the findbestmatch module works. Here is an example.from pywinauto.application import Application from pywinauto.findbestmatch import find_best_match ditto=Application().conn…

How to get the surface from a rect/line

I am trying to find the point where a line collides with a brick in the arkanoid that i am making. The most logical way i found is getting the mask from the line and use collidemask as it returns the p…

Python readin .txt and put in an array with numpy

i want to create an array with numpy. The base is a .txt file which is given in the following form:i tried it with loadtxt:data = np.loadtxt("myfile.txt",delimiter=\n,skiprows = 1)The first r…

Running a PyQt4 script without a display

I would like to run a Python script that normally opens a Qt window remotely over a connection with no X11 forwarding. Is there any way to create some kind of virtual display that the window drawing ca…

Scrapy : Program organization when interacting with secondary website

Im working with Scrapy 1.1 and I have a project where I have spider 1 scrape site A (where I aquire 90% of the information to fill my items). However depending on the results of the Site A scrape, I ma…