我有一个相当大的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 %组。
如果name中包含一个%元素,那么是否有方法从data["features"]中删除该元素,这样我就可以得到干净的json输出?在这个示例数据中,我应该只剩下data["features"]而不是空括号。