How to unencode a string

Discussion in 'Python' started by jakecjacobson, Aug 27, 2009.

  1. This seems like a real simple newbie question but how can a person
    unencode a string? In Perl I use something like: "$part=~ s/\%([A-Fa-
    f0-9]{2})/pack('C', hex($1))/seg;"

    If I have a string like Word1%20Word2%20Word3 I want to get Word1
    Word2 Word3. Would also like to handle special characters like '",(){}
    [] etc/
    jakecjacobson, Aug 27, 2009
    #1
    1. Advertising

  2. jakecjacobson

    Chris Rebert Guest

    On Thu, Aug 27, 2009 at 2:10 PM, jakecjacobson<> wrote:
    > This seems like a real simple newbie question but how can a person
    > unencode a string?  In Perl I use something like: "$part=~ s/\%([A-Fa-
    > f0-9]{2})/pack('C', hex($1))/seg;"
    >
    > If I have a string like Word1%20Word2%20Word3 I want to get Word1
    > Word2 Word3.  Would also like to handle special characters like '",(){}
    > [] etc/


    Use the `uu` module together with the `stringio` module:
    http://docs.python.org/library/uu.html
    http://docs.python.org/library/stringio.html

    Not sure about special character handling though.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Aug 27, 2009
    #2
    1. Advertising

  3. jakecjacobson

    MRAB Guest

    jakecjacobson wrote:
    > This seems like a real simple newbie question but how can a person
    > unencode a string? In Perl I use something like: "$part=~ s/\%([A-Fa-
    > f0-9]{2})/pack('C', hex($1))/seg;"
    >
    > If I have a string like Word1%20Word2%20Word3 I want to get Word1
    > Word2 Word3. Would also like to handle special characters like '",(){}
    > [] etc/


    The Python equivalent of your regular expression code is:

    >>> import re
    >>> s = "Word1%20Word2%20Word3"
    >>> re.sub(r"%([A-Fa-f0-9]{2})", lambda m: chr(int(m.group(1), 16)), s)

    'Word1 Word2 Word3'

    The replacement is a lambda expression (an anonymous function), which is
    called for each match with the match object and it works like this:

    m # m is the match object
    m.group(1) # get group 1 (the 2 hex digits)
    int(m.group(1), 16) # convert to an integer
    chr(int(m.group(1), 16)) # convert to a character
    MRAB, Aug 27, 2009
    #3
  4. >>>>> jakecjacobson <> (j) wrote:

    >j> This seems like a real simple newbie question but how can a person
    >j> unencode a string? In Perl I use something like: "$part=~ s/\%([A-Fa-
    >j> f0-9]{2})/pack('C', hex($1))/seg;"


    >j> If I have a string like Word1%20Word2%20Word3 I want to get Word1
    >j> Word2 Word3.


    urllib.unquote(string)

    >j> Would also like to handle special characters like '",(){}
    >j> [] etc/


    What would you like to do with them? Or do you mean to replace %27 by ' etc?
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Aug 27, 2009
    #4
  5. jakecjacobson

    Chris Rebert Guest

    On Thu, Aug 27, 2009 at 2:49 PM, Chris Rebert<> wrote:
    > On Thu, Aug 27, 2009 at 2:10 PM, jakecjacobson<> wrote:
    >> This seems like a real simple newbie question but how can a person
    >> unencode a string?  In Perl I use something like: "$part=~ s/\%([A-Fa-
    >> f0-9]{2})/pack('C', hex($1))/seg;"
    >>
    >> If I have a string like Word1%20Word2%20Word3 I want to get Word1
    >> Word2 Word3.  Would also like to handle special characters like '",(){}
    >> [] etc/

    >
    > Use the `uu` module together with the `stringio` module:
    > http://docs.python.org/library/uu.html
    > http://docs.python.org/library/stringio.html
    >
    > Not sure about special character handling though.


    Ah, I misread our post in haste. Please disregard my reply.

    - Chris
    Chris Rebert, Aug 28, 2009
    #5
  6. On Aug 27, 6:51 pm, Piet van Oostrum <> wrote:
    > >>>>> jakecjacobson <> (j) wrote:

    > >j> This seems like a real simple newbie question but how can a person
    > >j> unencode a string?  In Perl I use something like: "$part=~ s/\%([A-Fa-
    > >j> f0-9]{2})/pack('C', hex($1))/seg;"
    > >j> If I have a string like Word1%20Word2%20Word3 I want to get Word1
    > >j> Word2 Word3.  

    >
    > urllib.unquote(string)
    >
    > >j> Would also like to handle special characters like '",(){}
    > >j> [] etc/

    >
    > What would you like to do with them? Or do you mean to replace %27 by ' etc?
    > --
    > Piet van Oostrum <>
    > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
    > Private email:


    Yes, take '%27' and replace with ', etc.
    jakecjacobson, Aug 28, 2009
    #6
    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. Mladen Adamovic
    Replies:
    0
    Views:
    733
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,591
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    498
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,905
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,801
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page