[英]python 310 build dynamic url query dict from json response
How can I return a subset of the data
dictionary with dict comprehension?如何使用字典理解返回
data
字典的子集?
for example例如
from urllib.parse import urlencode, urlparse
data = [{"id": 123, "description": "big one", "productId": 1}, {"id":124, "description": "medium one", "productId": 2}, {"id": 125, "description": "small one", "productId": 3}]
url = urlparse("https://example.api.com/")
for elem in data:
query = urlencode(
query={"description": elem.get("description"), "productId": elem.get("productId")}
)
path = f"v1/product/{elem['id']}"
newurl = url._replace(path=path, query=query)
logger.debug(newurl.geturl())
My question is poorly worded.我的问题措辞不当。 I was hoping to get a subset of a dictionary using keys.
我希望使用键获得字典的子集。 Answer is here: Extract subset of key-value pairs from dictionary?
答案在这里: Extract subset of key-value pairs from dictionary?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.