Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I have this string in Python, how do I decode it?

s = 'Tara%2520Stiles%2520Living'

How do I turn it into:

Tara Stiles Living
like image 974
TIMEX Avatar asked Jan 05 '10 06:01

TIMEX


People also ask

How do you decode a string in Python?

decode() is a method specified in Strings in Python 2. This method is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string.

How do I decode a string?

So first, write the very first character of the encoded string and remove it from the encoded string then start adding the first character of the encoded string first to the left and then to the right of the decoded string and do this task repeatedly till the encoded string becomes empty.

What does encode () do in Python?

The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.


2 Answers

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).

like image 56
Greg Hewgill Avatar answered Sep 17 '22 16:09

Greg Hewgill


Use:

urllib.unquote(string)

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

like image 40
Sri Avatar answered Sep 19 '22 16:09

Sri