您好我需要将网址转换为路径,我得到的是这个网址如下:
url = u'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg'
以及看起来像这样的东西:
path = u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg'
THX。
答案 0 :(得分:16)
使用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.quote
或urllib.quote_plus
,您可以返回:
>>> urllib.quote(u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg')
'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg'
答案 1 :(得分:3)
如果您使用的是 Python3 ,则可以编写
urllib.parse.unquote(url)