URL Character Decoding

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

    #1

    URL Character Decoding

    If you have a link such as, e.g.:

    <a href="index.py? title=Main Menu">Main menu!</a>

    The space will be translated to the character code '%20' when you later
    retrieve the GET data. Not knowing if there was a library function that
    would convert these back to their actual characters, I've written the
    following:

    import re

    def sub_func(m):
    return chr(int(m.group ()[1:], 16))

    def parse_title(tit le):
    p = re.compile(r'%[0-9][0-9]')
    return re.sub(p, sub_func, title)

    (I know I could probably use a lambda function instead of sub_func, but
    I come to Python via C++ and am still not entirely used to them. This is
    clearer to me, at least.)

    I guess what I'm asking is: Is there a library function (in Python or
    mod_python) that knows how to do this? Or, failing that, is there a
    different regex I could use to get rid of the substitution function?

    -Kirk McDonald
  • Kirk McDonald

    #2
    Re: URL Character Decoding

    Kirk McDonald wrote:[color=blue]
    > If you have a link such as, e.g.:
    >
    > <a href="index.py? title=Main Menu">Main menu!</a>
    >
    > The space will be translated to the character code '%20' when you later
    > retrieve the GET data. Not knowing if there was a library function that
    > would convert these back to their actual characters, I've written the
    > following:
    >
    > import re
    >
    > def sub_func(m):
    > return chr(int(m.group ()[1:], 16))
    >
    > def parse_title(tit le):
    > p = re.compile(r'%[0-9][0-9]')
    > return re.sub(p, sub_func, title)
    >
    > (I know I could probably use a lambda function instead of sub_func, but
    > I come to Python via C++ and am still not entirely used to them. This is
    > clearer to me, at least.)
    >
    > I guess what I'm asking is: Is there a library function (in Python or
    > mod_python) that knows how to do this? Or, failing that, is there a
    > different regex I could use to get rid of the substitution function?
    >
    > -Kirk McDonald[/color]

    Actually, I just noticed this doesn't really work at all. The URL
    character codes are in hex, so not only does the regex not match what it
    should, but sub_func fails miserably. See why I wanted a library function?

    -Kirk McDonald

    Comment

    • Kirk McDonald

      #3
      Re: URL Character Decoding

      Kirk McDonald wrote:[color=blue]
      > Actually, I just noticed this doesn't really work at all. The URL
      > character codes are in hex, so not only does the regex not match what it
      > should, but sub_func fails miserably. See why I wanted a library function?
      >
      > -Kirk McDonald[/color]

      Not to keep talking to myself, but looks like sub_func works fine, and
      the regex just needs to be r'%[0-9a-fA-F][0-9a-fA-F]'. But even so.

      -Kirk McDonald

      Comment

      • Paul McGuire

        #4
        Re: URL Character Decoding

        "Kirk McDonald" <mooquack@suad. org> wrote in message
        news:43dd86ad@n ntp0.pdx.net...[color=blue]
        > If you have a link such as, e.g.:
        >
        > <a href="index.py? title=Main Menu">Main menu!</a>
        >
        > The space will be translated to the character code '%20' when you later
        > retrieve the GET data.
        >
        > I guess what I'm asking is: Is there a library function (in Python or
        > mod_python) that knows how to do this? Or, failing that, is there a
        > different regex I could use to get rid of the substitution function?
        >
        > -Kirk McDonald[/color]

        [color=blue][color=green][color=darkred]
        >>> import urllib
        >>> urllib.quote("i ndex.py?title=M ain Menu")[/color][/color][/color]
        'index.py%3Ftit le%3DMain%20Men u'[color=blue][color=green][color=darkred]
        >>> urllib.unquote( "index.py%3Ftit le%3DMain%20Men u")[/color][/color][/color]
        'index.py?title =Main Menu'


        Comment

        • Kirk McDonald

          #5
          Re: URL Character Decoding

          Paul McGuire wrote:[color=blue]
          > "Kirk McDonald" <mooquack@suad. org> wrote in message
          > news:43dd86ad@n ntp0.pdx.net...
          >[color=green]
          >>If you have a link such as, e.g.:
          >>
          >><a href="index.py? title=Main Menu">Main menu!</a>
          >>
          >>The space will be translated to the character code '%20' when you later
          >>retrieve the GET data.
          >>
          >>I guess what I'm asking is: Is there a library function (in Python or
          >>mod_python) that knows how to do this? Or, failing that, is there a
          >>different regex I could use to get rid of the substitution function?
          >>
          >>-Kirk McDonald[/color]
          >
          >
          >[color=green][color=darkred]
          >>>>import urllib
          >>>>urllib.quot e("index.py?tit le=Main Menu")[/color][/color]
          >
          > 'index.py%3Ftit le%3DMain%20Men u'
          >[color=green][color=darkred]
          >>>>urllib.unqu ote("index.py%3 Ftitle%3DMain%2 0Menu")[/color][/color]
          >
          > 'index.py?title =Main Menu'
          >
          >[/color]

          Perfect! Thanks.

          -Kirk McDonald

          Comment

          Working...