简体   繁体   中英

How to decode url to path in python, django

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.

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'

如果你使用Python3,你可以写

urllib.parse.unquote(url)

with python 3.9 and django 3.2 :

Solution 1 :

import urllib
urllib.parse.unquote(url)

Solution 2 :

from django.utils.encoding import uri_to_iri
uri_to_iri(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM