Alex
Alex

Reputation: 44749

Escaping HTML in Python

How to escape HTML with characters like – in Python?

Upvotes: 1

Views: 1173

Answers (2)

Abhishek Bera
Abhishek Bera

Reputation: 141

Try this

import cgi
print cgi.escape("<b>Your HTML Bold Text</b>")

Upvotes: 0

Martin v. L&#246;wis
Martin v. L&#246;wis

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>&#8211;</p>'

Upvotes: 2

Related Questions