Last active
April 22, 2020 12:55
-
-
Save joffilyfe/e93a3ab269e18eddf895dcee1cdd6490 to your computer and use it in GitHub Desktop.
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 html | |
_charref = re.compile(r'&(#[0-9]+;?' | |
r'|#[xX][0-9a-fA-F]+;?' | |
r'|[^\t\n\f <&#;]{1,32};?)') | |
def html_safe_decode(string, forbidden=["<", ">", "&"]): | |
""" | |
>>> "26. Cohen J. The Earth is Round (p.05>Am Psychol 1994; 49: 997-1003" | |
>>> html_safe_decode("26. Cohen J. The Earth is Round (p.05>Am Psychol 1994; 49: 997-1003") | |
""" | |
if '&' not in string: | |
return string | |
def replace_charref(s): | |
s = "&" + s.group(1) | |
if s in forbidden: | |
return s | |
return html.unescape(s) | |
return _charref.sub(replace_charref, string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
html_safe_decode("çã<&©>ˆ[]<")
'çã<&©>ˆ[]<'