Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: convert unicode character to corresponding Unicode string

Tags:

python

unicode

How do I convert a unicode character 'ב' to its corresponding Unicode character string '\u05d1' in Python?

I asked the opposite question a few days ago: Python: convert unicode string to corresponding Unicode character

like image 726
etayluz Avatar asked Dec 20 '25 20:12

etayluz


2 Answers

You can do something like,

>>> x
'ב'
>>> x.encode('ascii', 'backslashreplace').decode('utf-8')
'\\u05d1'

From the docs:

The errors parameter is the same as the parameter of the decode() method but supports a few more possible handlers. As well as 'strict', 'ignore', and 'replace' (which in this case inserts a question mark instead of the unencodable character), there is also 'xmlcharrefreplace' (inserts an XML character reference), backslashreplace (inserts a \uNNNN escape sequence) and namereplace (inserts a \N{...} escape sequence).

like image 91
han solo Avatar answered Dec 23 '25 09:12

han solo


Something like this works

>>> hex(ord('ב'))
'0x5d1'
like image 25
Vikram Cothur Avatar answered Dec 23 '25 09:12

Vikram Cothur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!