Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python3 – replacing non ascii characters to their unicode representative value?

lets say i have a string,
"Hello–World"

how would I convert it to something like this
"Hello\u2013World"
where "\u2013" is the unicode representative of "–"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use str.encode with unicode_escape:

>>> print(s.encode('unicode_escape'))
b'Hello\\u2013World'

If you want a string (and to a byte string like above):

>>> print(s.encode('unicode_escape').decode())
Hello\u2013World
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading