pythonurlencodeturbogearsturbogears2

Simple ascii url encoding with python


look at that:

import urllib
print urllib.urlencode(dict(bla='Ã'))

the output is

bla=%C3%BC

what I want is simple, I want the output in ascii instead of utf-8, so I need the output:

bla=%C3

if I try:

urllib.urlencode(dict(bla='Ã'.decode('iso-8859-1')))

doesn't work (all my python files are utf-8 encoded):

'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

In production, the input comes unicoded.


Solution

  • thanks to all solutions. all of you converge to the very same point. I made a mess changing the right code

    .encode('iso-8859-1') 
    

    to

    .decode('iso-8859-1')
    

    turn back to .encode('iso-8859-1') and it works.