Convert HTML entities to Unicode and vice versa

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.

Leave a Comment