htmlspecialchars() in Python

I was just looking for a way to unescape html entities in Python. Turns out this is not quite as simple as you might expect. Not as simple as PHP’s htmlspecialchars(), anyway. There is a translation table in htmlentitydefs, but you have to do the actual work yourself. Also, while that works for named entities, we want to support numeric ones as well.

I finally stumbled across this module, which contains a htmldecode() function that works very well: