Command line arguments


#1


python howlong2.py "allahabad" "mumbai"

"howlong2" is my file name.
errpr is :Traceback (most recent call last):
File "howlong2.py", line 9, in
print r["rows"][0]["elements"][0]["distance"]["text"]
KeyError: 'distance'


why this error is coming


Replace this line with your code. 
import requests
import json
#from pprint import pprint
r=requests.get('https://maps.googleapis.com/maps/api/distancematrix/json?origins=allahabad&destinations=mumbai')
r=r.json()
#print r
print r["rows"][0]["elements"][0]["distance"]["text"]
print r["rows"][0]["elements"][0]["duration"]["text"]
#data["om_points"]"""


#2

python howlong2.py "allahabad" "mumbai"
This is my input


#3

import requests
import sys
import json

from pprint import pprint

arglist=sys.argv
r=requests.get('https://maps.googleapis.com/maps/api/distancematrix/json?origins=arglist[1]&destinations=arglist[2]')
r=r.json()

print r

print r["rows"][0]["elements"][0]["distance"]["text"]
print r["rows"][0]["elements"][0]["duration"]["text"]

data["om_points"]"""

sorry this is the code"


#4

It's saying there's no distance key
Should there be? Perhaps you'll want to inspect the data or have a second look at what is promised about the data


#5

my above code is working fine but in this when taking command line arguments it is showing this error..i dont understand


#6

Well your code is kind of broken (formatting) so it's really hard to see what you're doing with the arguments.

If you're inserting them in the url then perhaps that's affecting the response in a way that there's no longer a distance key? Maybe you're not successfully adding the arguments, print out the url and have a look at what it was?


#7

import requests
import sys
import json

from pprint import pprint

arglist=sys.argv

r=requests.get('https://maps.googleapis.com/maps/api/distancematrix/json?origins=sys.argv[1]&destinations=sys.argv[2]')
r=r.json()

print r

print r["rows"][0]["elements"][0]["distance"]["text"]
print r["rows"][0]["elements"][0]["duration"]["text"]

data["om_points"]"""


#8

import requests
import sys
import json

r=requests.get('https://maps.googleapis.com/maps/api/distancematrix/json?origins=sys.argv[1]&destinations=sys.argv[2]')
r=r.json()

print r["rows"][0]["elements"][0]["distance"]["text"]
print r["rows"][0]["elements"][0]["duration"]["text"]


#9

There are both edit buttons and a preview window you know.. also there are formatting tools at the top of the post editor
There are also plenty of sites where you can paste it and link to.
Just make sure it's intact. That is, if copying it doesn't produce the same result, then you probably mangled it some way


#10

sorry i didnt notice , was in a hurry ,stuck at this problem for so long .


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.