开发者

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

开发者 https://www.devze.com 2022-12-15 06:25 出处:网络
s = \'Tara%2520Stiles%2520Living\' How do I turn it into: Tar开发者_C百科a Stiles Living You need to use urllib.unquote, but it appears you need to use it twice:

s = 'Tara%2520Stiles%2520Living'

How do I turn it into:

Tar开发者_C百科a Stiles Living


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


Use:

urllib.unquote(string)

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


>>> import urllib
>>> s = 'Tara%2520Stiles%2520Living'
>>> t=urllib.unquote_plus(s)
>>> print t
Tara%20Stiles%20Living
>>> urllib.unquote_plus(t)
'Tara Stiles Living'
>>>


import urllib

s = 'Tara%2520Stiles%2520Living'
t=''
while s<>t: s,t=t,urllib.unquote(s)


If you are using Python you should use urllib.parse.unquote(url) like the following code :

import urllib
url = "http://url-with-quoted-char:%3Cspan%3E%20%3C/span%3E"
print(url)
print(urllib.parse.unquote(url))

This code will output the following :

>>> print(url)
http://url-with-quoted-char:%3Cspan%3E%20%3C/span%3E
>>> print(urllib.parse.unquote(url))
http://url-with-quoted-char:<span> </span>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号