html escape sequences

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will McGugan

    #1

    html escape sequences

    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
  • Leif K-Brooks

    #2
    Re: html escape sequences

    Will McGugan wrote:[color=blue]
    > 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?[/color]

    How about this?

    import re
    from htmlentitydefs import name2codepoint

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

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

    def handle_html_ent ities(string):
    return _entity_re.sub( _repl_func, string)

    Comment

    • Will McGugan

      #3
      Re: html escape sequences

      Leif K-Brooks wrote:[color=blue]
      > Will McGugan wrote:
      >[color=green]
      >> 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?[/color]
      >
      >
      > How about this?
      >
      > import re
      > from htmlentitydefs import name2codepoint
      >
      > _entity_re = re.compile(r'&( ?:(#)(\d+)|([^;]+));')
      >
      > def _repl_func(matc h):
      > if match.group(1): # Numeric character reference
      > return unichr(int(matc h.group(2)))
      > else:
      > return unichr(name2cod epoint[match.group(3)])
      >
      > def handle_html_ent ities(string):
      > return _entity_re.sub( _repl_func, string)[/color]

      muchas gracias!

      Will McGugan

      Comment

      Working...