27

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.

3 Answers 3

33

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'
Sign up to request clarification or add additional context in comments.

2 Comments

Note that in Python 3 this is in the module urllib.parse.
@Humphrey, The question is tagged python-2.7.
18

If you are using Python3 you can write

urllib.parse.unquote(url)

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.