Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 我可以将dict转换为url吗?_Python 2.7_Url_Urlparse - Fatal编程技术网

Python 2.7 我可以将dict转换为url吗?

Python 2.7 我可以将dict转换为url吗?,python-2.7,url,urlparse,Python 2.7,Url,Urlparse,例如,我有以下url模式: url_pattern = { "scheme": "http", "netloc": "for.example.com", "path": "/ex1", "params": "", "query": a=b&c=d, "fragment": "" } 这就像urlparse.urlparse(“http://for.example.com/ex1?a=b&c=d)。我可以从目录中获取url吗 def get

例如,我有以下url模式:

url_pattern = {
    "scheme": "http",
    "netloc": "for.example.com",
    "path": "/ex1",
    "params": "",
    "query": a=b&c=d,
    "fragment": ""
}
这就像
urlparse.urlparse(“http://for.example.com/ex1?a=b&c=d)
。我可以从目录中获取url吗

def geturl(pattern):
    return pattern["scheme"] + "://" + pattern["netloc"] + pattern["path"] + ("?" if pattern["query"]) + pattern["query"]

应该可以很好地工作。

urlparse
生成一个
ParseResult
对象。因此,只需构造一个
ParseResult
对象,并使用
geturl()
方法生成URL

>>> url_pattern = {
    "scheme": "http",
    "netloc": "for.example.com",
    "path": "/ex1",
    "params": "",
    "query": "a=b&c=d",
    "fragment": ""
}
>>> from urlparse import ParseResult
>>> ParseResult(**url_pattern).geturl()
'http://for.example.com/ex1?a=b&c=d'

对使用
urllib.urlencode()