This is because you are not declaring the encoding your file uses, so Python infers it from the current locale configuration. I suggest you do this:
# -*- coding: utf-8 -*- import urllib mystring = "î" print urllib.quote(mystring) print urllib.quote_plus(mystring)
And make sure your file.py gets to utf-8 encoded disk .
For me it gives:
$python ex.py %C3%AE %C3%AE
A few caveats. If you are trying to do this from the interpreter, # -*- coding: utf-8 -*- will not work if your console encoding is not utf-8 . Instead, you should change it to any encoding used by your console: # -*- coding: (encoding here) -*- .
Then you should decode your string in Unicode using the decode method and pass it the encoding name used by your console as an argument:
mystring = "î".decode('<your encoding>')
Then go to urllib encoded as utf-8 :
print urllib.quote(mystring.encode('utf-8')) print urllib.quote_plus(mystring.encode('utf-8'))
Hope this helps!
source share