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?
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'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With