Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 3.6中获取存储在列表中的字典项_Python_Json_Dictionary - Fatal编程技术网

如何在python 3.6中获取存储在列表中的字典项

如何在python 3.6中获取存储在列表中的字典项,python,json,dictionary,Python,Json,Dictionary,我有一个包含不同词典的列表 [{'header-name': 'x-frame-options', 'ignore': False, 'expected-value': ['deny', 'sameorigin']}, {'header-name': 'content-security-policy', 'ignore': False, 'expected-value': []}] 我使用这个方法,从字典中提取所有字段 def use_custom_setting(self, hea

我有一个包含不同词典的列表

[{'header-name': 'x-frame-options', 'ignore': False, 'expected-value': ['deny', 'sameorigin']}, {'header-name': 'content-security-policy', 'ignore': False, 'expected-value': []}]

我使用这个方法,从字典中提取所有字段

    def use_custom_setting(self, header, contents):
        warn = 1
        if header == "x-frame-options":
            for value in self.custom:
                for keys, values in value.items():
                    if keys == "header-name" and values == "x-frame-options":
                        print(keys,values)
                        if keys== "ignore" and values == False: # i dont reach here 
                            if keys == "expected-value":
                                print(values)
                                if contents.lower() in values:
                                    warn = 0
                                else:
                                    warn = 1
                            print(values)
                        else:
                            print("This header is skipped based on configure file")

        return {'defined': True, 'warn': warn, 'contents': contents}

我的目标是获取标题名内容并忽略内容和期望值内容?

第二个if语句永远不会为true,因为它嵌套在第一个if语句中。这意味着只有当外部值为真时才对其进行评估。第一个也一样。想想看:

第一个if语句检查
keys
是否等于
“header name”
,第二个if语句再次检查
keys
是否等于
“ignore”
。当然,如果它有值
“header name”
,它也不会是
“ignore”

您需要取消嵌套我们的if语句:

for keys, values in value.items():
    if keys == "header-name" and values == "x-frame-options":
        print(keys,values)
    if keys== "ignore" and values == False: # i dont reach here 
        print(keys,values)
    if keys == "expected-value":
        print(keys,values)
编辑:您需要稍微分离您的关注点。逻辑的一部分(我在这里提供的部分)只是用于确定您正在查看的键。然后,您可以继续使用它们包含的值进行操作:

ignoreItem = False # This variable is reset for every dictionary
for keys, values in value.items():
    if keys== "ignore": # i dont reach here 
        ignoreItem = values
    if keys == "expected-value" and ignoreItem:
        print(keys,values)
注意我是如何存储
“ignore”
的值的,下一次循环运行时,我使用该存储值来确定是否打印
“expected value”

顺便说一下,整个结构有点奇怪。如果字典的键是已知的,为什么不直接使用它们:

if value["header-name"] == "x-frame-options":
    if value["ignore"] == False:
        print(value["expected-value"]) # Do your stuff here

我得到了它,但我只需要在ignore为false时得到期望值。使用您的解决方案,我将始终获得预期值:/@Ibrahim查看编辑。我还提出了一种完全不同的方法,我现在就试试,谢谢你的提示,我是python新手,所以我对语法有点困惑,所有的Hanks@都能完美地工作!