[英]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 转换为路径,我得到的是这个 url,如下所示:
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: 使用urllib.unquote
解码%
-encoded字符串:
>>> 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
或urllib.quote_plus
,你可以回来:
>>> 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 :使用python 3.9和django 3.2 :
import urllib
urllib.parse.unquote(url)
from django.utils.encoding import uri_to_iri
uri_to_iri(url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.