Reputation: 1268
For example:
>>> u = u'€€€'
>>> s = u.encode('utf8')
>>> s
'\xe2\x82\xac\xe2\x82\xac\xe2\x82\xac'
>>> print s
€€€
But I want to get the string:
"%E2%82%AC%E2%82%AC%E2%82%AC"
as is sometimes required for URLs.
Currently I'm doing it byte by byte, as in:
>>> "%0X" % ord(u.encode('utf8')[0])
'E2'
Is there a simpler / more elegant way to do this?
Upvotes: 4
Views: 973
Reputation: 1382
Look to the quote function from urllib module http://docs.python.org/2/library/urllib.html#urllib.quote
>>> import urllib
>>> u = u'€€€'
>>> s = u.encode('utf-8')
>>> print urllib.quote(s)
%E2%82%AC%E2%82%AC%E2%82%AC
Upvotes: 3
Reputation: 1702
You can try to use urllib2 module.
import urllib2
s = '\xe2\x82\xac\xe2\x82\xac\xe2\x82\xac'
urllib2.quote(s)
Output:
'%E2%82%AC%E2%82%AC%E2%82%AC'
Upvotes: 4