Skip to content

Instantly share code, notes, and snippets.

@nriley
Created August 31, 2011 20:01
Show Gist options
  • Save nriley/1184554 to your computer and use it in GitHub Desktop.
Save nriley/1184554 to your computer and use it in GitHub Desktop.
Unescape for XML
import re, htmlentitydefs
# from http://effbot.org/zone/re-sub.htm#unescape-html
def unescape(text):
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
return re.sub("&#?\w+;", fixup, text)
print unescape('für').encode('ascii', 'xmlcharrefreplace')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment