views:

161

answers:

3

s = 'Tara%2520Stiles%2520Living'

How do I turn it into:

Tara Stiles Living
+2  A: 

Use:

urllib.unquote(string)

http://docs.python.org/library/urllib.html

Srirangan
this answer isn't complete! the other two answers are.
Peter
I was just providing reference to the package / method that needed to be used. Wasn't posting a "fix".
Srirangan
@Srirangan, then consider the downvotes and the comment a suggestion to the OP that he should unaccept this one and accept a different one that actually answers the question correctly. (Or you could update yours, so that at least the accepted answer is a full answer.)
Peter Hansen
@Peter, sure @Alex, do as Peter says and tick mark and award up-votes to Ghostdog or Greg :P
Srirangan
While the other 2 answers are correct for this instance, how do you determine if when you no longer need to unquote? What if it had been triple encoded?
Tony Lenzi
@Tony Lenzi, with this type of escaping, there's no possible way to know for certain whether a string is encoded, so you can't tell when you've decoded it "completely". You'd either have to know how many times it has been encoded, or you'd be guessing and in some (possibly not likely) cases you'd be "decoding" data that wasn't actually encoded.
Peter Hansen
+14  A: 

You need to use urllib.unquote, but it appears you need to use it twice:

>>> import urllib
>>> s = 'Tara%2520Stiles%2520Living'
>>> urllib.unquote(urllib.unquote(s))
'Tara Stiles Living'

After unquoting once, your "%2520" turns into "%20", which unquoting again turns into " " (a space).

Greg Hewgill
+3  A: 
>>> import urllib
>>> s = 'Tara%2520Stiles%2520Living'
>>> t=urllib.unquote_plus(s)
>>> print t
Tara%20Stiles%20Living
>>> urllib.unquote_plus(t)
'Tara Stiles Living'
>>>
ghostdog74