Stuck with urllib.quote and Unicode/UTF-8

Marian Steinbach marian at sendung.de
Sat May 7 08:24:33 EDT 2011


Hi!

I am stuck with calling URLs with parameters containing non-ASCII
characters. I'm creating a url like this.

  url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' +
urllib.quote(address) + '&sensor=false&language=DE'

address can be a string like u"Köln, Nordrhein-Westfalen". This results in

  http://maps.googleapis.com/maps/api/geocode/json?address=K%F6ln%2C%20Nordrhein-Westfalen&sensor=false&language=DE

and this doesn't seem to meet Google's Encoding expectations. I get an
"INVALID REQUEST" response. (The same things works if there are only
ASCII characters in the address.)

When I manually enter the "Köln"... string in the Firefox address bar,
Firefox makes the URL

  http://maps.googleapis.com/maps/api/geocode/xml?address=K%C3%B6ln,%20Nordrhein-Westfalen&sensor=false&language=DE

out of this and it works fine. %C3%B6 seems to be the url-encoded
representation of an UTF-8 representation of a "ö" character.


So the next thing I tried is to get address utf-8-encoded, like this:

  url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' +
urllib.quote(address.encode('utf-8')) + '&sensor=false&language=DE'

This gives me an error like

  UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position
1: ordinal not in range(128)

Again, the input string for address is u"Köln, Nordrhein-Westfalen".


Can you see what I'm doing wrong or what I would have to to to get
from u"ö" to "%C3%B6"?


Thanks!

Marian



More information about the Python-list mailing list