UTF-8 percentage encoding and python

python, url-encoding, utf-8

Solution

Your file has to encode your string as `utf-8` before quoting it, and the string should be unicode. Also you have to specify the appropriate file encoding for your source file in the `coding` section:

# -*- coding: utf-8 -*-

import urllib

s = u'î'
print urllib.quote(s.encode('utf-8'))

Gives me the output:

%C3%AE

Problem

I'm trying to get python to give me percent encoded strings. The API I'm interacting with (which I think is using percent encoded UTF-8), gives %c3%ae for î. However, python's urllib.quote gives %3F. ``` import urllib mystring = "î" print urllib.quote(mystring) print urllib.quote_plus(mystring) print urllib.quote(mystring.encode('utf-8')) ``` Any help appreciated.

Original source