Oscalation
Oscalation

Reputation: 380

Python JSON cant pull out and print textual identifier

I am attempting to pull out and print the place ID from this http://python-data.dr-chuck.net/geojson

Here is my code:

import urllib
import json

serviceurl = 'http://python-data.dr-chuck.net/geojson'

while True:
    address = raw_input('Enter location: ')
    if len(address) < 1 : break

    url = serviceurl + '?' + urllib.urlencode({'sensor':'false', 'address':  address})
    print 'Retrieving', url
    uh = urllib.urlopen(url)
    data = uh.read()
    print 'Retrieved',len(data),'characters'

    try: 
        js = json.loads(str(data))
    except: 
        js = None
        if 'status' not in js or js['status'] != 'OK':
            print '==== Failure To Retrieve ===='
            print data
            continue
    print json.dumps(js, indent=4)
    placeid = js['results'][0]['place_id']

    print "Place ID: ", placeid
    print placeid
    print location 

The output im getting is correct, but the place ID is not being printed. Any ideas as to why?

Upvotes: 3

Views: 195

Answers (2)

tdelaney
tdelaney

Reputation: 77367

You don't check whether the user's location was recognized by the system. When you enter a bad location, you get an error key and a list of valid locations. Just check for that.

import urllib
import json

serviceurl = 'http://python-data.dr-chuck.net/geojson'

while True:
    address = raw_input('Enter location: ')
    if len(address) < 1 : break

    url = serviceurl + '?' + urllib.urlencode({'sensor':'false', 'address':  address})
    print 'Retrieving', url
    uh = urllib.urlopen(url)
    data = uh.read()
    # uncomment for debug...
    #print 'Retrieved',len(data),'characters'

    try: 
        js = json.loads(str(data))
    except:
        js = None
        print 'got exception'
        continue

    if "error" in js:
        print "Choose a location from the following list"
        for location in js["locations"]:
            print '   ', location
        continue

    if 'status' not in js or js['status'] != 'OK':
        print '==== Failure To Retrieve ===='
        print data
        continue
    print json.dumps(js, indent=4)
    placeid = js['results'][0]['place_id']

    print "Place ID: ", placeid
    print placeid

Upvotes: 0

Stidgeon
Stidgeon

Reputation: 2723

Same as noted in the comments - it works if you change your variable name:

import urllib
import json

serviceurl = 'http://python-data.dr-chuck.net/geojson'

while True:
    address = raw_input('Enter location: ')
    if len(address) < 1 : break

    url = serviceurl + '?' + urllib.urlencode({'sensor':'false', 'address':  address})
    print 'Retrieving', url
    uh = urllib.urlopen(url)
    data = uh.read()
    print 'Retrieved',len(data),'characters'

    try: js = json.loads(str(data))
    except: js = None
    if 'status' not in js or js['status'] != 'OK':
        print '==== Failure To Retrieve ===='
        print data
        continue
    #print json.dumps(js, indent=4) # commented out to stop filling my screen with place names - easily put back in
    placeid = js['results'][0]['place_id']

    print "Place ID: ", placeid
    print placeid
    print address    # location not defined 

The important bit is that last line, which changes the variable name to be the one you declared in the request for user input.

I used 'University of Toronto' as a test, without problems.

Upvotes: 2

Related Questions