The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Geocode error
Discuss Geocode error in the Python Programming forum on Dev Shed. Geocode error Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 8th, 2013, 10:52 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
|
Geocode help!
So I'm using the Google Geocoding API to obtain the ZIP code when given an address such as 'Lowell Observatory, Flagstaff, AZ' and it returns the zip code of the address as a string '86001'.
I finally got it to print out the json output, but I'm not sure how to go about getting just the zip code and returning only that and nothing else.
Here's my code:
Code:
def get_zipcode(address):
zipcode = ''
# +++your code here+++
url="http://maps.googleapis.com/maps/api/geocode/json?address=" + urllib.quote(address.encode('utf-8')) + "&sensor=false"
response = urllib2.urlopen(url).read()
print response
Or would it be easier to go the xml path?
Here's my code for that:
Code:
def get_zipcode(address):
zipcode = ''
# +++your code here+++
url="http://maps.googleapis.com/maps/api/geocode/xml?address=" + urllib.quote(address.encode('utf-8')) + "&sensor=false"
response = urllib2.urlopen(url).read()
tree = ET.parse(response)??
root = tree.getroot()??
return root
For the xml code, it's kind of weird b/c I get an error (IOError: [Errno 2] No such file or directory) but it still returns the xml output...
If someone could show me how to go about it either way or both, I would really appreciate it 
|

February 9th, 2013, 09:45 AM
|
 |
Contributing User
|
|
|
|
This should give you a hint ...
Code:
''' jason_decode2dict1.py
extracting data from a json object
tested with Python273
'''
import urllib
import urllib2
import json
address = "Lowell Observatory, Flagstaff, AZ"
url1 = "http://maps.googleapis.com/maps/api/geocode/json?address="
url2 = url1 + urllib.quote(address.encode('utf-8')) + "&sensor=false"
# this will be a json string object
response = urllib2.urlopen(url2).read()
print(type(response)) # <type 'str'>
# decode json string object to Python dictionary object
jdecode = json.JSONDecoder()
response2 = jdecode.decode(response)
print(type(response2)) # <type 'dict'>
# fish out the address
address = response2['results'][0]['formatted_address']
print(address)
''' result ...
Lowell Observatory, 1400 West Mars Hill Road, Flagstaff, AZ 86001, USA
'''
# now fish out the zipcode
print('-'*40)
address_list = address.split(',')
print(address_list)
print(address_list[-2])
print(address_list[-2].split())
# finally ...
zipcode = address_list[-2].split()[1]
print(zipcode)
''' result ...
[u'Lowell Observatory', u' 1400 West Mars Hill Road', u' Flagstaff', u' AZ 86001', u' USA']
AZ 86001
[u'AZ', u'86001']
86001
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
Last edited by Dietrich : February 9th, 2013 at 05:02 PM.
|

February 9th, 2013, 02:42 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
I'm getting an error on the jdecode part for some reason.
ValueError: No JSON object could be decoded.
Also, after looking through the json code and whatnot, I think xml output might be easier for me? At least to grasp better - so I tried it out that way, and got it to print out what I want.
Thanks!
|

February 9th, 2013, 04:57 PM
|
 |
Contributing User
|
|
|
|
Looks like xml is a little easier in your case ...
Code:
''' xml_get_data2.py
extracting data from a xml object
tested with Python273
'''
import urllib
import urllib2
import xml.etree.ElementTree as ET
#address = "Lowell Observatory, Flagstaff, AZ"
address = "Disneyland, Anaheim, CA"
url1 = "http://maps.googleapis.com/maps/api/geocode/xml?address="
url2 = url1 + urllib.quote(address.encode('utf-8')) + "&sensor=false"
# this will be a xml string object
xml_response = urllib2.urlopen(url2).read()
root = ET.fromstring(xml_response)
comp_list = [code for code in root.findall(".//address_component") \
if code.findtext('type') == 'postal_code']
zipcode = [element.find('long_name').text for element in comp_list]
print(zipcode)
print(zipcode[0])
|

February 9th, 2013, 05:04 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 27
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
|
|
|
Yep! Thanks for all your help Dietrich!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|