如果我在 Python 中有这个字符串,我该如何解码它?

发布于 2024-08-17 04:22:28 字数 100 浏览 2 评论 0原文

s = 'Tara%2520Stiles%2520Living'

如何将其变成:

Tara Stiles Living

s = 'Tara%2520Stiles%2520Living'

How do I turn it into:

Tara Stiles Living

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不会写诗 2024-08-24 04:22:28

您需要使用 urllib.unquote ,但看来您需要使用它两次

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

取消引用一次后,“%2520”变成“%20”,再次取消引用变成“”(空格)。

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

開玄 2024-08-24 04:22:28
>>> 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=urllib.unquote_plus(s)
>>> print t
Tara%20Stiles%20Living
>>> urllib.unquote_plus(t)
'Tara Stiles Living'
>>>
有深☉意 2024-08-24 04:22:28
import urllib

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

s = 'Tara%2520Stiles%2520Living'
t=''
while s<>t: s,t=t,urllib.unquote(s)
听不够的曲调 2024-08-24 04:22:28

如果您使用 Python,则应该使用 urllib.parse.unquote(url),如以下代码:

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

此代码将输出以下内容:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文