How do you htmlentities in Python

Cameron Laird claird at lairds.us
Mon Jun 4 18:54:53 CEST 2007


In article <1180965792.757685.132580 at q75g2000hsh.googlegroups.com>,
Adam Atlas  <adam at atlas.st> wrote:
>As far as I know, there isn't a standard idiom to do this, but it's
>still a one-liner. Untested, but I think this should work:
>
>import re
>from htmlentitydefs import name2codepoint
>def htmlentitydecode(s):
>    return re.sub('&(%s);' % '|'.join(name2codepoint), lambda m:
>name2codepoint[m.group(1)], s)
>

A.  I *think* you meant
        import re
        from htmlentitydefs import name2codepoint
        def htmlentitydecode(s):
            return re.sub('&(%s);' % '|'.join(name2codepoint), lambda m: chr(name2codepoint[m.group(1)]), s)
    We're stretching the limits of what's comfortable
    for me as a one-liner.
B.  How's it happen this isn't in the Cookbook?  I'm
    curious about what other Pythoneers think:  is 
    this better memorialized in the Cookbook or the
    Wiki?



More information about the Python-list mailing list