Python如果值存在,则从json中删除元素

时间:2017-03-30 15:47:11

标签: python json geojson

我有一个相当大的geojson文件,它是从一些国家气象服务数据转换而来的。我在这里将它修剪成这个样本:

{
  "properties": {
    "name": "day1otlk"
  },
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.71424459627099,
              40.229695635383166
            ],
            [
              -122.62484780364827,
              40.53410620541074
            ],
            [
              -122.71424459627099,
              40.229695635383166
            ]
          ]
        ]
      },
      "properties": {
        "Name": "General Thunder",
        "stroke-opacity": 1,
        "stroke-width": 4,
        "name": "General Thunder",
        "fill": "#c0e8c0",
        "fill-opacity": 0.75,
        "stroke": "#ffffff",
        "timeSpan": {
          "end": "2017-03-30T12:00:00Z",
          "begin": "2017-03-29T20:00:00Z"
        }
      },
      "type": "Feature"
    },
    {
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -108.65861565996833,
              32.91391108773154
            ],
            [
              -108.63932601964923,
              32.95521185698698
            ],
            [
              -108.65861565996833,
              32.91391108773154
            ]
          ]
        ]
      },
      "properties": {
        "Name": "General Thunder",
        "stroke-opacity": 1,
        "stroke-width": 4,
        "name": "General Thunder",
        "fill": "#c0e8c0",
        "fill-opacity": 0.75,
        "stroke": "#ffffff",
        "timeSpan": {
          "end": "2017-03-30T12:00:00Z",
          "begin": "2017-03-29T20:00:00Z"
        }
      },
      "type": "Feature"
    },
    {
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -92.67280213157608,
              38.47870651780003
            ],
            [
              -92.62448390998837,
              38.45534960370862
            ],
            [
              -92.59475154780039,
              38.493327413824595
            ],
            [
              -92.64308574626148,
              38.51669676139087
            ],
            [
              -92.67280213157608,
              38.47870651780003
            ]
          ]
        ]
      },
      "properties": {
        "Name": "10 %",
        "stroke-opacity": 1,
        "stroke-width": 4,
        "name": "10 %",
        "fill": "#8b4726",
        "fill-opacity": 0.89,
        "stroke": "#ffffff",
        "timeSpan": {
          "end": "2017-03-30T12:00:00Z",
          "begin": "2017-03-29T20:00:00Z"
        }
      },
      "type": "Feature"
    },
    {
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -92.67280213157608,
              38.47870651780003
            ],
            [
              -92.62448390998837,
              38.45534960370862
            ],
            [
              -92.59475154780039,
              38.493327413824595
            ],
            [
              -92.64308574626148,
              38.51669676139087
            ],
            [
              -92.67280213157608,
              38.47870651780003
            ]
          ]
        ]
      },
      "properties": {
        "Name": "10 %",
        "stroke-opacity": 1,
        "stroke-width": 4,
        "name": "20 %",
        "fill": "#8b4726",
        "fill-opacity": 0.89,
        "stroke": "#ffffff",
        "timeSpan": {
          "end": "2017-03-30T12:00:00Z",
          "begin": "2017-03-29T20:00:00Z"
        }
      },
      "type": "Feature"
    },
    {
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -97.09845994557838,
              38.43843745045377
            ],
            [
              -97.07114801649661,
              38.47751978088534
            ],
            [
              -97.09845994557838,
              38.43843745045377
            ]
          ]
        ]
      },
      "properties": {
        "Name": "5 %",
        "stroke-opacity": 1,
        "stroke-width": 4,
        "name": "5 %",
        "fill": "#b47f00",
        "fill-opacity": 0.89,
        "stroke": "#ffffff",
        "timeSpan": {
          "end": "2017-03-30T12:00:00Z",
          "begin": "2017-03-29T20:00:00Z"
        }
      },
      "type": "Feature"
    }
  ]
}

我希望删除name中有%的元素。我不想要那些坐标或任何包含的东西。

这是我的代码:

import json

with open('test.geojson') as data_file:
    data = json.load(data_file)

for element in data["features"]:
    if '%' in element["properties"]["name"]:
        del element["type"]
        del element["properties"] # Deletes the properties
        del element["geometry"] # Deletes the  coords


with open('test_output.geojson', 'w') as data_file:
    data = json.dump(data, data_file)

这足以删除元素的子键,但我留下的输出看起来像:

{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}

我也尝试过使用

for element in data["features"]:
        if '%' in element["properties"]["name"]:
                    data["features"].remove(element)

但似乎只删除了样本组中的最后一个元素,即5 %组。它应该删除10 %20 %5 %组。

如果data["features"]中有一个name,那么有没有办法从%中移除元素,所以我留下了干净的json输出?在此示例数据中,我应该留下的唯一data["features"]General Thunder,没有空括号。

2 个答案:

答案 0 :(得分:2)

使用del element["type"]del element["properties"]del element["geometry"]的问题在于它只会从该元素的属性dict中删除这些项目。不是元素本身。

对于你的第二项,当你在for element in data["features"]:之类的列表上进行迭代时,在迭代它时修改列表或对象是不好的(这是data["features"].remove(element)发生的事情)。此外,list.remove()会删除具有的项目。因此element在值上下文中使用,而不是该元素

最好创建一个新列表,然后分配它。你能做的是:

new_features = []
for element in data["features"]:
    if '%' not in element["properties"]["name"]:    # note the 'not'
        new_features.append(element)                # new_features has the one's you want
# and then re-assign features to the list with the elements you want
data["features"] = new_features

答案 1 :(得分:2)

使用简单的过滤器:

no_percent = lambda feature: '%' not in feature['properties']['name']
data['features'] = filter(no_percent, data['features'])

或者作为列表理解:

data['features'] = [feature for feature in data['features']
                      if '%' not in feature['properties']['name']]