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 :
import urllib
urllib.parse.unquote(url)
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.