Parsing string containing Unicode character names

123 Views Asked by At

I have a string

>>> s
u'M\\N{AMPERSAND}M\\N{APOSTROPHE}s'
>>> print s
M\N{AMPERSAND}M\N{APOSTROPHE}s

How do I turn it into M&M's?

2

There are 2 best solutions below

0
On BEST ANSWER

You can use unicode_escape encoding:

In Python 2.x:

>>> u'M\\N{AMPERSAND}M\\N{APOSTROPHE}s'.decode('unicode-escape')
u"M&M's"

In Python 3.x:

>>> u'M\\N{AMPERSAND}M\\N{APOSTROPHE}s'.encode().decode('unicode-escape')
"M&M's"
0
On

Seeing your print command I assume you are working in Python 2. You can use decode() with an input which is the encoding you are using i.e. in this case 'unicode-escape'

>>> s
u'M\\N{AMPERSAND}M\\N{APOSTROPHE}s'
>>>s.decode('unicode-escape')
>>> print s
"M&M's"