Is there a similar or equivalent function in Python to the PHP function htmlspecialchars()? The closest thing I've found so far is htmlentitydefs.entitydefs().
-
1It seems that there is more than one obvious way to do it! O noes! – Grant Paul Mar 25 '10 at 04:35
8 Answers
from django.utils.html import escape
print escape('<div class="q">Q & A</div>')

- 330
- 4
- 4
-
1I'm voting for this because I don't want to parse anything like some of the other answers, or even do a search and replace, I want a single function that does it all for me. – paulmorriss Jun 18 '10 at 15:36
Building on @garlon4 answer, you can define your own htmlspecialchars(s)
:
def htmlspecialchars(text):
return (
text.replace("&", "&").
replace('"', """).
replace("<", "<").
replace(">", ">")
)

- 1,576
- 19
- 22
-
I think python has a fancy function named something like "translate" that you could use to make this even shorter – Brian Peterson Feb 07 '20 at 06:25
-
Too lazy right now but yeah: https://www.programiz.com/python-programming/methods/string/translate – Brian Peterson Feb 07 '20 at 06:27
-
Helpful answer, however you're passing the parameters to replace() in the wrong order. Should be: replace("string to find", "string to replace") – Ben Apr 25 '21 at 08:06
-
@Ben no, the function works as expected (it escapes the "html special chars"). It looks for the char to escape, and replaces it by the html escape sequence for that char. Maybe you wanted to un-escape instead? – AlejandroVD Apr 26 '21 at 20:01
-
I think the simplest way is just to use replace:
text.replace("&", "&").replace('"', """).replace("<", "<").replace(">", ">")
PHP only escapes those four entities with htmlspecialchars. Note that if you have ENT_QUOTES set in PHP, you need to replace quotes with ' rather than ".

- 1,162
- 10
- 14
You probably want xml.sax.saxutils.escape:
from xml.sax.saxutils import escape
escape(unsafe, {'"':'"'}) # ENT_COMPAT
escape(unsafe, {'"':'"', '\'':'''}) # ENT_QUOTES
escape(unsafe) # ENT_NOQUOTES
Have a look at xml.sax.saxutils.quoteattr, it might be more useful for you

- 7,147
- 27
- 36
Only five characters need to be escaped, so you can use a simple one-line function:
def htmlspecialchars(content):
return content.replace("&", "&").replace('"', """).replace("'", "'").replace("<", "<").replace(">", ">")

- 7,332
- 3
- 48
- 69

- 11
- 2
The html.entities
module (htmlentitydefs
for python 2.x) contains a dictionary codepoint2name
which should do what you need.
>>> import html.entities
>>> html.entities.codepoint2name[ord("&")]
'amp'
>>> html.entities.codepoint2name[ord('"')]
'quot'

- 96,888
- 11
- 64
- 71
If you are using django 1.0 then your template variables will already be encoded and ready for display. You also use the safe
operator {{ var|safe }}
if you don't want it globally turned on.

- 48,968
- 59
- 172
- 213