Created
November 6, 2009 15:11
-
-
Save hktechn0/228037 to your computer and use it in GitHub Desktop.
Replace HTML character entity reference
This file contains hidden or 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
import re | |
import htmlentitydefs | |
def replace_htmlentity(string): | |
amp = string.find('&') | |
if amp == -1: | |
return string | |
entity = re.compile("&([A-Za-z]+);") | |
entity_match = entity.findall(string) | |
for name in entity_match: | |
try: | |
c = htmlentitydefs.name2codepoint[name] | |
except KeyError: | |
continue | |
string = string.replace("&%s;" % name, unichr(c)) | |
return string | |
print replace_htmlentity("< >&&hogehoge;") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment