Yan King Yin
Yan King Yin

Reputation: 1268

Convert Unicode string to a hexadecimal escape sequence using Python

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

Answers (2)

Dmitry Vakhrushev
Dmitry Vakhrushev

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

Puffin GDI
Puffin GDI

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

Related Questions