Category Archives: Python

Encode and Decode URLs in Python for Google Appengine

While developing in Python for Google Appengine you’ll might want to encode or decode URLs. Sounds like a simple task, as it is in many other languages. Somehow in Python 2.5.x which is the version supported by appengine, it’s not as straight forward, at least it wasn’t for me. There are tones solutions, suggestions and examples out there, not all work as expected.

After some trial and error it finally worked:

import urllib

text = 'some text'

#decodeURI
text = urllib.unquote(text.encode('ascii')).decode('utf-8')

#encodeURI
text = urllib.quote(text.encode('utf-8'))

Might spare you some time.

If your gonna work with unicode on your appengine app than your in for some other troubles. This presentation, and this article (and its comments) might help a bit.