How to decode url to path in python, django

2019-06-15 00:14发布

Hi I need to convert url to path, what i got is this url as bellow:

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

and what to be looked something like this:

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

thx.

1条回答
\"骚年 ilove
2楼-- · 2019-06-15 01:02

Use urllib.unquote to decode %-encoded string:

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

Using urllib.quote or urllib.quote_plus, you can get back:

>>> urllib.quote(u'/static/media/uploads/gallery/Marrakech, Morocco_be3Ij2N.jpg')
'/static/media/uploads/gallery/Marrakech%2C%20Morocco_be3Ij2N.jpg'
查看更多
登录 后发表回答