Re: How to unencode a string
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: p...@vanoostrum.org Yes, take '%27' and replace with ', etc. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to unencode a string
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to unencode a string
> 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: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list
Re: How to unencode a string
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/ import urllib print urllib.unquote("Word1%20Word2%20Word3") -- Stephen Fairchild -- http://mail.python.org/mailman/listinfo/python-list
Re: How to unencode a string
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to unencode a string
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 -- http://mail.python.org/mailman/listinfo/python-list
How to unencode a string
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/ -- http://mail.python.org/mailman/listinfo/python-list