Geocoding Strategies - Python 2.x to Python 3.x

Leb

I'm trying to get some basic access to the Google Maps API. In their Geocoding Strategies they have an example shown on how to access it using Python:

import urllib2

address="1600+Amphitheatre+Parkway,+Mountain+View,+CA"
url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" % address

response = urllib2.urlopen(url)
jsongeocode = response.read()

I converted it to python 3.4, but the data that I'm obtaining is not in a valid JSON format. An alternative route is to use BeautifulSoup, but I'm not trying to do anything complicated.

This is the code that I'm using with Python 3.4:

import urllib.request
import json

address="1600+Amphitheatre+Parkway,+Mountain+View,+CA"
url="https://maps.googleapis.com/maps/api/geocode/json?address=%s" % address

with urllib.request.urlopen(url) as link:
    s = str(link.read())

print(json.dumps(s))

This is a snapshot of the data I'm receiving:

"b'{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"1600\",\n
\"short_name\" : \"1600\",

erewok

The problem you are having is that you are taking binary data and turning it into a string by wrapping it with str. The proper way to turn binary data into a string in Python3 is to decode it.

Thus, you should instead decode your results into a string before attempting to interpret them as json (Python3.4 example):

with urllib.request.urlopen(url) as link:
    s = link.read()
    result = json.loads(s.decode('utf-8'))

But I also recommend giving requests a shot because it can simplify these steps for you:

>>> import requests
>>> resp = requests.get(url)
>>> resp.json()
{'status': 'OK', 'results': [{'types': ['street_address'], 'place_id': 'ChIJ2eUgeAK6j4ARbn5u_wAGqWA', 'address_components': [{'types': ['street_number'], 'long_name': '1600', 'short_name': '1600'}, {'types': ['route'], 'long_name': 'Amphitheatre Parkway', 'short_name': 'Amphitheatre Pkwy'}, {'types': ['locality', 'political'], 'long_name': 'Mountain View', 'short_name': 'Mountain View'}, {'types': ['administrative_area_level_2', 'political'], 'long_name': 'Santa Clara County', 'short_name': 'Santa Clara County'}, {'types': ['administrative_area_level_1', 'political'], 'long_name': 'California', 'short_name': 'CA'}, {'types': ['country', 'political'], 'long_name': 'United States', 'short_name': 'US'}, {'types': ['postal_code'], 'long_name': '94043', 'short_name': '94043'}], 'formatted_address': '1600 Amphitheatre Parkway, Mountain View, CA 94043, USA', 'geometry': {'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 37.4236854802915, 'lng': -122.0828811197085}, 'southwest': {'lat': 37.4209875197085, 'lng': -122.0855790802915}}, 'location': {'lat': 37.4223365, 'lng': -122.0842301}}}]}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: Eval with undefined variables (2*x+x = 3*x)

From Java

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Script incompatibility for Python 2.x and Python 3.x

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Regex unicode in python 2.x vs 3.x

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Accessing Python2.x and 3.x in windows

From Java

Using both Python 2.x and Python 3.x in IPython Notebook

From Dev

How to parse Python 2.x with Python 3.x ast module?

From Dev

Can't import turtle module in Python 2.x and Python 3.x

From Dev

string is not same in Python3x as for Python2.x but Still it is working

From Dev

Generator evaluation difference between Python 2.x and Python 3.x

From Dev

random.randint shows different output in Python 2.x and Python 3.x with same seed

From Dev

Change "Edit with IDLE" default to python 3.X instead of python 2.X on windows

From Dev

How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

From Dev

PyYAML with Python 3.x

From Dev

Pyinstaller with Python3.x

From Dev

Upgrading to python 3.x

From Dev

PyYAML with Python 3.x

From Dev

Summing a 2d array in Python 3.x

From Dev

Is it possible to create GTK+2.x applications using Python 3?

From Dev

Function with args and default kwargs for Python 2.x and 3.x

From Dev

How can I get 2.x-like sorting behaviour in Python 3.x?

From Dev

Valid syntax in both Python 2.x and 3.x for raising exception?

From Dev

Difference in multithreading overhead between python 2.x and 3.x

From Dev

Differences between input commands in Python 2.x and 3.x

From Dev

Troubleshooting Unicode string behavior when updating Python 2.x->3.x

From Dev

Setting up Django environments for Python2.X and 3.X?

Related Related

  1. 1

    Python: Eval with undefined variables (2*x+x = 3*x)

  2. 2

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  3. 3

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  4. 4

    Script incompatibility for Python 2.x and Python 3.x

  5. 5

    Deal with buffer in Python 2.x and 3.x

  6. 6

    Regex unicode in python 2.x vs 3.x

  7. 7

    Deal with buffer in Python 2.x and 3.x

  8. 8

    Accessing Python2.x and 3.x in windows

  9. 9

    Using both Python 2.x and Python 3.x in IPython Notebook

  10. 10

    How to parse Python 2.x with Python 3.x ast module?

  11. 11

    Can't import turtle module in Python 2.x and Python 3.x

  12. 12

    string is not same in Python3x as for Python2.x but Still it is working

  13. 13

    Generator evaluation difference between Python 2.x and Python 3.x

  14. 14

    random.randint shows different output in Python 2.x and Python 3.x with same seed

  15. 15

    Change "Edit with IDLE" default to python 3.X instead of python 2.X on windows

  16. 16

    How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

  17. 17

    PyYAML with Python 3.x

  18. 18

    Pyinstaller with Python3.x

  19. 19

    Upgrading to python 3.x

  20. 20

    PyYAML with Python 3.x

  21. 21

    Summing a 2d array in Python 3.x

  22. 22

    Is it possible to create GTK+2.x applications using Python 3?

  23. 23

    Function with args and default kwargs for Python 2.x and 3.x

  24. 24

    How can I get 2.x-like sorting behaviour in Python 3.x?

  25. 25

    Valid syntax in both Python 2.x and 3.x for raising exception?

  26. 26

    Difference in multithreading overhead between python 2.x and 3.x

  27. 27

    Differences between input commands in Python 2.x and 3.x

  28. 28

    Troubleshooting Unicode string behavior when updating Python 2.x->3.x

  29. 29

    Setting up Django environments for Python2.X and 3.X?

HotTag

Archive