python中删除并输出元素28_如果值存在,Python将元素从json中移除

我有一个相当大的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"]而不是空括号。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值