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"]而不是空括号。
我有一个相当大的geojson文件,它是从一些国家气象服务数据转换而来的。我把它简化成这个样本:{"properties": {"name": "day1otlk"},"type": "FeatureCollection","features": [{"geometry": {"type": "Polygon","coordinates": [[[-122.71424459627099,40.22...
Out[2]: [0, 1, 2, 4, 5, 6, 7, 8, 9]
2、关于del
del list[ ] 根据列表的索引
值
(也即
元素
所在的位置)对列表的
元素
进行
删除
;切片式
删除
。
##
删除
第2到4个
元素
a = [x