error parsing result

86 views
Skip to first unread message

Van Tu

unread,
Mar 22, 2013, 2:57:53 AM3/22/13
to ge...@googlegroups.com
Hi spent awhile trying to figure out why my code was stopping randomly. Figured out it was an error parsing some of the results. Can anyone can be of assistance and tell me how to parse results that have special characters? when i try to do place, (lat, lng) = result and the result happens to have special characters it fails such as the following example. 
Skövde, Sweden

Mike Tigas

unread,
Mar 22, 2013, 11:59:26 AM3/22/13
to ge...@googlegroups.com
I don’t seem to have this issue with plain String objects:

>>> from geopy import geocoders
>>> g = geocoders.GoogleV3()
>>> g.geocode("Skövde, Sweden")
(u'Sk\xf6vde, Sweden', (58.3902782, 13.8461208))
>>> place, (lat, lng) = g.geocode("Skövde, Sweden")
>>> print place
Skövde, Sweden
>>> print lat, lng
58.3902782 13.8461208

-----

However, I notice that using a Unicode object appears to fail:

>>> input_str = u"Skövde, Sweden"
>>> print input_str
Skövde, Sweden
>>> type(input_str)
<type 'unicode'>
>>> g.geocode(input_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mtigas/Code/geopy/repo/geopy/geocoders/googlev3.py",
line 129, in geocode
url = self.get_url(params)
File "/Users/mtigas/Code/geopy/repo/geopy/geocoders/googlev3.py",
line 84, in get_url
{'domain': self.domain, 'params': urlencode(params)})
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",
line 1312, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in
position 2: ordinal not in range(128)

-----

I'll fix this in a 0.95.1 update today, but if you can't wait, you can
force your Unicode objects to be UTF-8 Strings:

>>> if isinstance(input_str, unicode):
... input_str = input_str.encode('utf-8')
...
>>> g.geocode(input_str)
(u'Sk\xf6vde, Sweden', (58.3902782, 13.8461208))

Hope this helps.

Mike Tigas
Knight-Mozilla OpenNews Fellow, ProPublica
https://mike.tig.as/ | http://www.propublica.org/
@mtigas | GChat: mi...@tig.as | Skype: mike.tigas
> --
> You received this message because you are subscribed to the Google Groups
> "geopy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to geopy+un...@googlegroups.com.
> To post to this group, send email to ge...@googlegroups.com.
> Visit this group at http://groups.google.com/group/geopy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages