How to unencode a string

J

jakecjacobson

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/
 
C

Chris Rebert

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
 
M

MRAB

jakecjacobson said:
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
 
P

Piet van Oostrum

jakecjacobson said:
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?
 
C

Chris Rebert

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
 
J

jakecjacobson

jakecjacobson <[email protected]> (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?

Yes, take '%27' and replace with ', etc.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top