html escape sequences

Discussion in 'Python' started by Will McGugan, Mar 18, 2005.

  1. Will McGugan

    Will McGugan Guest

    Hi,

    I'd like to replace html escape sequences, like &nbsp and &#39 with
    single characters. Is there a dictionary defined somewhere I can use to
    replace these sequences?

    Thanks,

    Will McGugan
    Will McGugan, Mar 18, 2005
    #1
    1. Advertising

  2. Will McGugan wrote:
    > I'd like to replace html escape sequences, like &nbsp and &#39 with
    > single characters. Is there a dictionary defined somewhere I can use to
    > replace these sequences?


    How about this?

    import re
    from htmlentitydefs import name2codepoint

    _entity_re = re.compile(r'&(?:(#)(\d+)|([^;]+));')

    def _repl_func(match):
    if match.group(1): # Numeric character reference
    return unichr(int(match.group(2)))
    else:
    return unichr(name2codepoint[match.group(3)])

    def handle_html_entities(string):
    return _entity_re.sub(_repl_func, string)
    Leif K-Brooks, Mar 18, 2005
    #2
    1. Advertising

  3. Will McGugan

    Will McGugan Guest

    Leif K-Brooks wrote:
    > Will McGugan wrote:
    >
    >> I'd like to replace html escape sequences, like &nbsp and &#39 with
    >> single characters. Is there a dictionary defined somewhere I can use
    >> to replace these sequences?

    >
    >
    > How about this?
    >
    > import re
    > from htmlentitydefs import name2codepoint
    >
    > _entity_re = re.compile(r'&(?:(#)(\d+)|([^;]+));')
    >
    > def _repl_func(match):
    > if match.group(1): # Numeric character reference
    > return unichr(int(match.group(2)))
    > else:
    > return unichr(name2codepoint[match.group(3)])
    >
    > def handle_html_entities(string):
    > return _entity_re.sub(_repl_func, string)


    muchas gracias!

    Will McGugan
    Will McGugan, Mar 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olaf Petzold

    vhdl textio and escape sequences

    Olaf Petzold, Nov 26, 2005, in forum: VHDL
    Replies:
    1
    Views:
    3,391
    Mike Treseler
    Nov 28, 2005
  2. Harald Kirsch
    Replies:
    0
    Views:
    321
    Harald Kirsch
    Nov 19, 2004
  3. Kit

    Escape sequences and printing

    Kit, Sep 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    487
    Matt Gregory
    Sep 17, 2003
  4. harrelson
    Replies:
    3
    Views:
    3,880
    Craig Ringer
    Dec 10, 2004
  5. slomo
    Replies:
    5
    Views:
    1,506
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page