Python – Convert HTML entities to Unicode and vice versa

htmlhtml-entitiespython

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

Best Solution

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.