Skip to content

Instantly share code, notes, and snippets.

@Dih5
Created July 4, 2017 09:56
Show Gist options
  • Save Dih5/511aa88421739e8b0d471edb15558d31 to your computer and use it in GitHub Desktop.
Save Dih5/511aa88421739e8b0d471edb15558d31 to your computer and use it in GitHub Desktop.
Python method to escape html using entities
from html.entities import codepoint2name
def html_escape(text):
d = dict((chr(code), '&%s;' % name) for code, name in codepoint2name.items() if code!=38) # exclude "&"
text = text.replace("&", "&")
for key, value in d.items():
text = text.replace(key, value)
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment