Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Unicode string to a hexadecimal escape sequence using Python

Tags:

python

unicode

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?

like image 676
Yan King Yin Avatar asked Mar 21 '23 21:03

Yan King Yin


2 Answers

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'
like image 169
Puffin GDI Avatar answered Apr 06 '23 11:04

Puffin GDI


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
like image 37
Dmitry Vakhrushev Avatar answered Apr 06 '23 10:04

Dmitry Vakhrushev