Skip to content

Instantly share code, notes, and snippets.

@hackjutsu
Last active January 23, 2017 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackjutsu/211179a4846abab9178a3b85cfb84ace to your computer and use it in GitHub Desktop.
Save hackjutsu/211179a4846abab9178a3b85cfb84ace to your computer and use it in GitHub Desktop.
[HTML encoding/decoding via Python standard library] Reference: http://stackoverflow.com/a/7088472
# HTML Encoding
try:
from html import escape # python 3.x
except ImportError:
from cgi import escape # python 2.x
print(escape("<"))
# HTML Decoding
try:
from html import unescape # python 3.4+
except ImportError:
try:
from html.parser import HTMLParser # python 3.x (<3.4)
except ImportError:
from HTMLParser import HTMLParser # python 2.x
unescape = HTMLParser().unescape
print(unescape("&gt;"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment