Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2013, 10:52 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 9th, 2013, 09:45 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
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.

Reply With Quote
  #3  
Old February 9th, 2013, 02:42 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #4  
Old February 9th, 2013, 04:57 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
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])

Reply With Quote
  #5  
Old February 9th, 2013, 05:04 PM
taeBaby taeBaby is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 27 taeBaby User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 19 m 39 sec
Reputation Power: 0
Yep! Thanks for all your help Dietrich!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Geocode error

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap