Tural Gurbanov
Tural Gurbanov

Reputation: 742

Convert string from xmlcharrefreplace back to utf-8

I've next part of code:

In [8]: st = u"опа"

In [11]: st.encode("ascii", "xmlcharrefreplace")
Out[11]: 'опа'

In [14]: st1 = st.encode("ascii", "xmlcharrefreplace")

In [15]: st1.decode("ascii", "xmlcharrefreplace")
Out[15]: u'опа'

In [16]: st1.decode("utf-8", "xmlcharrefreplace")
Out[16]: u'опа'

Do you have any idea how to convert st1 back to u"опа"?

Upvotes: 12

Views: 12146

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125068

Use the html.unescape() function (Python 3.4 and newer):

>>> import html
>>> html.unescape('опа')
'опа'

On older versions (including Python 2), you’d have to use an instance of HTMLParser.HTMLParser():

>>> from HTMLParser import HTMLParser
>>> parser = HTMLParser()
>>> parser.unescape('опа')
u'\u043e\u043f\u0430'
>>> print parser.unescape('опа')
опа

Upvotes: 23

Related Questions