Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.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
从JSON文件(Python)中过滤出所需的数据_Python_Json_Dictionary - Fatal编程技术网

从JSON文件(Python)中过滤出所需的数据

从JSON文件(Python)中过滤出所需的数据,python,json,dictionary,Python,Json,Dictionary,这是我的json文件的一个示例: { "pops": [{ "name": "pop_a", "subnets": { "Public": ["1.1.1.0/24,2.2.2.0/24"], "Private": ["192.168.0.0/24,192.168.1.0/24"], "more DATA":"" } }, { "name": "po

这是我的json文件的一个示例:

{
"pops": [{
        "name": "pop_a",
        "subnets": {
            "Public": ["1.1.1.0/24,2.2.2.0/24"],
            "Private": ["192.168.0.0/24,192.168.1.0/24"],
        "more DATA":""  
        }
    },
    {
        "name": "pop_b",
        "subnets": {
            "Public": ["3.3.3.0/24,4.4.4.0/24"],
            "Private": ["192.168.2.0/24,192.168.3.0/24"],
        "more DATA":""
        }
    }
]
}
在我阅读之后,我想制作一个dic对象,并存储我需要从这个文件中获得的一些东西。 我希望我的目标是这样的

[{
    "name": "pop_a",
    "subnets": {"Public": ["1.1.1.0/24,2.2.2.0/24"],"Private": ["192.168.0.0/24,192.168.1.0/24"]}
},
{
    "name": "pop_b",
    "subnets": {"Public": ["3.3.3.0/24,4.4.4.0/24"],"Private": ["192.168.2.0/24,192.168.3.0/24"]}
}]
然后我希望能够访问一些公共/私人价值观

这是我尝试过的,我知道update()、setdefault()也会给出同样不想要的结果

def my_funckion():
   nt_json = [{'name':"",'subnets':[]}]
   Pname = []
   Psubnet= []

   for pop in pop_json['pops']:  # it print only the last key/value
      nt_json[0]['name']= pop['name']
      nt_json[0]['subnet'] = pop['subnet']
   pprint (nt_json)

  for pop in pop_json['pops']: 
   """
     it print the names in a row then all of the ipss
   """
     Pname.append(pop['name'])
     Pgre.append(pop['subnet'])
     nt_json['pop_name'] = Pname
     nt_json['subnet']= Psubnet
   pprint (nt_json)

下面是一个使用列表理解的快速解决方案。请注意,只有充分了解json结构,才能采用这种方法

>>> import json
>>>
>>> data = ... # your data
>>> new_data = [{ "name" : x["name"], "subnets" : {"Public" : x["subnets"]["Public"], "Private" : x["subnets"]["Private"]}} for x in data["pops"]]
>>>
>>> print(json.dumps(new_data, indent=2))
[
  {
    "name": "pop_a",
    "subnets": {
      "Private": [
        "192.168.0.0/24,192.168.1.0/24"
      ],
      "Public": [
        "1.1.1.0/24,2.2.2.0/24"
      ]
    }
  },
  {
    "name": "pop_b",
    "subnets": {
      "Private": [
        "192.168.2.0/24,192.168.3.0/24"
      ],
      "Public": [
        "3.3.3.0/24,4.4.4.0/24"
      ]
    }
  }
]

你到底想得到什么样的输出?您得到了什么您不需要在将json文件导入变量后格式化它。只需使用for循环即可找到所需的键。然后使用嵌套for循环查找嵌套dict中的键。第二个数据结构在我看来非常像第一个数据结构-那么,您需要更改什么?谢谢Coldspeed它为我工作了。您能解释一下indent=2的作用吗?您可以指定用于缩进json的空格数。根级别=0个空间,级别1=2个空间,级别2=4个空间。。。等等如果你把缩进改为3,那么它是0,3,6。。。等等。谢谢,我如何从新的\u数据中打印特定的pop。。或者只有流行名字。。。我不能这么做…@IssaDaniel目前你的最终结构是一个列表。如果您的POP名称是唯一的,您可以考虑将您的最终结构转换为以POP名称作为关键字的列表。其余的保持不变。