def replace_entities(ustring, placeholder="<STR_LIT:U+0020>"):
def _repl_func(match):<EOL><INDENT>try:<EOL><INDENT>if match.group(<NUM_LIT:1>): <EOL><INDENT>return unichr( int(match.group(<NUM_LIT:2>)) ) <EOL><DEDENT>else:<EOL><INDENT>try: return cp1252[ unichr(int(match.group(<NUM_LIT:3>))) ].strip()<EOL>except: return unichr( name2codepoint[match.group(<NUM_LIT:3>)] )<EOL><DEDENT><DEDENT>except:<EOL><INDENT>return placeholder<EOL><DEDENT><DEDENT>if not isinstance(ustring, unicode):<EOL><INDENT>ustring = UnicodeDammit(ustring).unicode<EOL><DEDENT>ustring = ustring.replace("<STR_LIT>", "<STR_LIT:U+0020>")<EOL>_entity_re = re.compile(r'<STR_LIT>') <EOL>return _entity_re.sub(_repl_func, ustring)<EOL>
Replaces HTML special characters by readable characters.

    As taken from Leif K-Brooks algorithm on:
    http://groups-beta.google.com/group/comp.lang.python