views:

305

answers:

1

How to escape HTML with characters like – in Python?

+2  A: 

If you have a unicode string as input, you can use the xmlcharrefreplace error handler:

py> u"<p>\N{EN DASH}</p>".encode("ascii", "xmlcharrefreplace")
'<p>&#8211;</p>'
Martin v. Löwis