1

How to escape HTML with characters like – in Python?

4
  • See this previous Stack Overflow question: stackoverflow.com/questions/913933/… Commented Aug 8, 2009 at 15:48
  • 1
    Not to be confused with "Escaping FROM Pythons!" Commented Aug 8, 2009 at 15:49
  • @Alex, please clarify: do you have a Python Unicode string and want to produce the escaped HTML, or viceversa, do you have the HTML containing escapes and want to produce a Python Unicode string? Commented Aug 8, 2009 at 16:07
  • @Alex, I'd like to produce a Python Unicode string from HTML with escapes ASCII string, well, and the other way round as well. Commented Aug 9, 2009 at 11:59

2 Answers 2

2

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>'
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.