gpt4 book ai didi

python - 如何在python,django中将url解码为路径

转载 作者:IT老高 更新时间:2023-10-28 21:13:09 25 4
gpt4 key购买 nike

嗨,我需要将 url 转换为路径,我得到的是这个 url,如下所示:

url = u'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg'

以及看起来像这样的东西:

path = u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg'

谢谢。

最佳答案

使用 urllib.unquote解码 % 编码的字符串:

>>> import urllib
>>> url = u'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg'
>>> urllib.unquote(url)
u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg'

使用 urllib.quoteurllib.quote_plus ,你可以回来:

>>> urllib.quote(u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg')
'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg'

关于python - 如何在python,django中将url解码为路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com