我想作为检索数据
{id:{分支:"xxx","项目":"某个项目"}
想要获得以上的数据
无法创建嵌套词典
其中代码给出了列表中的最后一个值
from urllib.parse import urlsplit, parse_qs
import requests
from requests.auth import HTTPBasicAuth
import json
url_list=["http://xxxxxxxxx/+/502789","http://xxxxxxxxx/+/502799"]
gerrit_review_id=[]
branch = {}
project = {}
link ={}
#final_data={}
#enter code here
values=[]
sample={}
for url in url_list:
project_path = urlparse(url).path.split("+")
permalink=project_path[1].strip("/")
gerrit_review_id.append(permalink)
#print((gerrit_review_id))
for id in gerrit_review_id:
response = requests.get(f"http://xxxxx/{id}",auth = HTTPBasicAuth('xxxxxx','xxxxx'))
payload=(response.text[4:]) // getting the bad characters so removed
gerrit_data =json.loads(payload)
sam=('branch','project','_number')
for items in gerrit_data.items():
#final_data={}
for i in sam:
final_data={}
if i in gerrit_data.keys():
final_data[id]=gerrit_data[i]
print(final_data)
对于隐藏的url,我无法测试该文件。
对于您的问题,python中的字典被定义为{key: value}
,其中value
也可以是字典。
{id:{"branch:"xxx","project":"someproject"}
简单地变成:
my_dict = {
"id":{
"branch":"xxx",
"project":"someproject"
}
}
现在要访问project
的值,我们需要首先访问该值为内容的字典。
nested_dict = my_dict["id"]
然后我们可以访问project
的值
value = nested_dict["project"]
注意,我们只需编写my_dict["id"]["project"]
就可以访问project