Convert HTML entities to Unicode and vice versa

hekevintran picture hekevintran · Mar 31, 2009 · Viewed 75.3k times · Source

Possible duplicates:

How do you convert HTML entities to Unicode and vice versa in Python?

Answer

Isaac picture Isaac · Apr 17, 2010

As to the "vice versa" (which I needed myself, leading me to find this question, which didn't help, and subsequently another site which had the answer):

u'some string'.encode('ascii', 'xmlcharrefreplace')

will return a plain string with any non-ascii characters turned into XML (HTML) entities.