[英]How to get result from a dictionary with lists as the values
I have a JSON file with n number of dictionaries as listed below in the snippet.我有一个 JSON 文件,其中包含 n 个字典,如下面的片段中所列。 I am trying to fetch the value against the key but it fails in my code when the value is defined as a list like in the below example for key
affected_packages
.我正在尝试针对键获取值,但是当值被定义为列表时,它在我的代码中失败,如下面的 key
affected_packages
_packages 示例所示。 I tried to check why my code fails, so it looks like it pulls no data out of it this fails.我试图检查我的代码失败的原因,所以看起来它没有从中提取任何数据,这失败了。 I just see two brackets
[]
as output instead of "thunderbird-0:78.9.1-1.el8_1","thunderbird-0:78.9.1-1.el8_2","thunderbird-0:78.9.1-1.el8_3","thunderbird-0:78.9.1-1.el7_9"
我只看到两个括号
[]
为 output 而不是"thunderbird-0:78.9.1-1.el8_1","thunderbird-0:78.9.1-1.el8_2","thunderbird-0:78.9.1-1.el8_3","thunderbird-0:78.9.1-1.el7_9"
{"bugzilla_description":"CVE-2021-23992 Mozilla: A crafted OpenPGP key with an invalid user ID could be used to confuse the user","cvss_score":null,"cvss_scoring_vector":null,"CWE":"CWE-347","affected_packages":["thunderbird-0:78.9.1-1.el8_1","thunderbird-0:78.9.1-1.el8_2","thunderbird-0:78.9.1-1.el8_3","thunderbird-0:78.9.1-1.el7_9"],"resource_url":"https://access.redhat.com/hydra/rest/securitydata/cve/CVE-2021-23992.json","cvss3_scoring_vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:N/I:N/A:L","cvss3_score":"4.3"}
I am doing like below in my code as I need to prepare a worksheet.我在我的代码中执行如下操作,因为我需要准备一个工作表。 Sample snippet:
示例片段:
for i in range(offs):
ws.cell(row=r+1+i,column=2).value = v['current'][i]
if 'affected_packages' in list(tmp1.keys()):
ws.cell(row=r+1+index1,column=11).value = tmp1['affected_packages']
print("affected_packages done")
if 'advisories' in list(tmp1.keys()):
ws.cell(row=r+1+index2,column=13).value = tmp1['advisories']
print("advisories done")
Is there a way I can pull the value correctly for those defined as a list in the dictionary?有没有一种方法可以正确提取字典中定义为列表的值? I need a way so that it won't hamper my existing logic to pull value for normal key: value since while looking up into my JSON file.
我需要一种方法,这样它就不会妨碍我现有的逻辑来提取普通键的值:值,因为在查看我的 JSON 文件时。
So need something which can fulfil both whether my value in the dictionary is as a list or not as a list and I can get value against the keys in my json file.因此,无论我在字典中的值是作为列表还是不作为列表,都需要一些东西,并且我可以根据我的 json 文件中的键获得值。
As mentioned in the other answers, you can test the type of a variable using如其他答案中所述,您可以使用
if type(some_variable) == list:
# do what you need to do
You do mention that your code breaks, and I guess it's because inserting into a cell expects a String, not the list you pass in the line您确实提到您的代码中断了,我猜这是因为插入单元格需要一个字符串,而不是您在行中传递的列表
ws.cell(row=r+1+index1,column=11).value = tmp1['affected_packages']
So how do we get a string out of a list of strings?那么我们如何从字符串列表中得到一个字符串呢? It's pretty easy using the join method.
使用 join 方法非常简单。
my_list = ["thunderbird-0:78.9.1-1.el8_1","thunderbird-0:78.9.1-1.el8_2","thunderbird-0:78.9.1-1.el8_3","thunderbird-0:78.9.1-1.el7_9"]
as_one_string = ", ".join(my_list)
print(as_one_string)
# Prints out 'thunderbird-0:78.9.1-1.el8_1, thunderbird-0:78.9.1-1.el8_2, thunderbird-0:78.9.1-1.el8_3, thunderbird-0:78.9.1-1.el7_9'
So combining the two ideas:所以结合这两个想法:
if 'affected_packages' in list(tmp1.keys()):
ws.cell(row=r+1+index1,column=11).value = tmp1['affected_packages'] if type(tmp1['affected_packages']) != list else ", ".join(tmp1['affected_packages'])
print("affected_packages done")
Quick feedback because I can't comment yet: Please always include an error message and/or the output you get when running your code when you ask a question快速反馈,因为我还不能发表评论:请始终包含错误消息和/或在您提出问题时运行代码时收到的 output
Regarding your second problem, when you don't know if it is a list or something else, you can just check the type, maybe like this:关于您的第二个问题,当您不知道它是列表还是其他内容时,您可以检查类型,可能是这样的:
if type(tmp1['affected_packages']) == list:
# process the list
else:
# process other types
Since you don't know the data type, having this explicit type check seems necessary.由于您不知道数据类型,因此似乎有必要进行此显式类型检查。
If I understand it correctly, you just need to determine if a value in dict is list.如果我理解正确,您只需要确定 dict 中的值是否为列表。 You can do that as below:
你可以这样做:
for i in d.items(): # gets key, value as a tuple.
if isinstance(i[1],list):
print('its a list, process it accordingly')
else:
print('Not a list')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.