HTML Encoded Translation

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

    #1

    HTML Encoded Translation

    How can I translate this:

    gi

    to this:

    "gi"

    I've tried urllib.unencode and it doesn't work.

    Thanks!

  • Fredrik Lundh

    #2
    Re: HTML Encoded Translation

    Dave wrote:
    How can I translate this:
    >
    gi
    >
    to this:
    >
    "gi"
    the easiest way is to run it through an HTML or XML parser (depending on
    what the source is). or you could use something like this:

    import re

    def fix_charrefs(te xt):
    def fixup(m):
    text = m.group(0)
    try:
    if text[:3] == "&#x":
    return unichr(int(text[3:-1], 16))
    else:
    return unichr(int(text[2:-1]))
    except ValueError:
    pass
    return text # leave as is
    return re.sub("&#?\w+; ", fixup, text)
    >>fix_charrefs( "g&#10 5;")
    'gi'

    also see:


    I've tried urllib.unencode and it doesn't work.
    those are HTML/XML character references, not encoded URL characters.

    </F>

    Comment

    • Sybren Stuvel

      #3
      Re: HTML Encoded Translation

      Dave enlightened us with:
      How can I translate this:
      >
      &#103;&#105;
      >
      to this:
      >
      "gi"
      >
      I've tried urllib.unencode and it doesn't work.
      As you put so nicely in the subject: it is HTML encoding, not URL
      encoding. Those are two very different things! Try a HTML decoder,
      you'll have more luck with that...

      Sybren
      --
      Sybren Stüvel
      Stüvel IT - http://www.stuvel.eu/

      Comment

      • Dave

        #4
        Re: HTML Encoded Translation

        Got it, great. This worked like a charm. I knew I was barking up the
        wrong tree with urllib, but I didn't know which tree to bark up...

        Thanks!

        Fredrik Lundh wrote:
        Dave wrote:
        >
        How can I translate this:

        &#103;&#105;

        to this:

        "gi"
        >
        the easiest way is to run it through an HTML or XML parser (depending on
        what the source is). or you could use something like this:
        >
        import re
        >
        def fix_charrefs(te xt):
        def fixup(m):
        text = m.group(0)
        try:
        if text[:3] == "&#x":
        return unichr(int(text[3:-1], 16))
        else:
        return unichr(int(text[2:-1]))
        except ValueError:
        pass
        return text # leave as is
        return re.sub("&#?\w+; ", fixup, text)
        >
        >>fix_charrefs( "&#103;&#10 5;")
        'gi'
        >
        also see:
        >

        >
        I've tried urllib.unencode and it doesn't work.
        >
        those are HTML/XML character references, not encoded URL characters.
        >
        </F>

        Comment

        Working...