Reputation: 44749
How to escape HTML with characters like –
in Python?
Upvotes: 1
Views: 1173
Reputation: 141
Try this
import cgi
print cgi.escape("<b>Your HTML Bold Text</b>")
Upvotes: 0
Reputation: 127587
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>–</p>'
Upvotes: 2