Created
July 4, 2017 09:56
-
-
Save Dih5/511aa88421739e8b0d471edb15558d31 to your computer and use it in GitHub Desktop.
Python method to escape html using entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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