为了账号安全,请及时绑定邮箱和手机立即绑定

如何过滤掉其中包含区域字段的字典并放入新的 json 文件

如何过滤掉其中包含区域字段的字典并放入新的 json 文件

守着星空守着你 2022-01-18 15:41:40
{ “SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group13918.jpg61884”:{ “文件名”: “SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group13918.jpg”, “大小”:61884, “区”:[{ “shape_attributes”:{ “名称”: “多边形”, “all_points_x”:[181,175,170,179,179,196,185,185,203,222,214,182] “all_points_y”:[176,182,198,182,192,211,189,177,172,165,163,175]}, “region_attributes”:{}},{ “shape_attributes”:{ “名称”: “多边形”, “all_points_x”:[173,151,144,154,173,175,173,174,181,187,220,212,178,173], “all_points_y”:[134,142,156,145,139,148,158,164,141,137,134,130,134,132]},” region_attributes":{}},{"shape_attributes":{"name":"多边形”, “all_points_x”:[205,184,177,182,181,187,186,216,228,218,190,198,209,216,208,195,202,206,207], “all_points_y”:[215,262,244,264,271,277,268,281,282,275,265,257,252,247,245,255,235,221,214]}, “region_attributes”:{}},{ “shape_attributes”:{ “名称”: “多边形”, “all_points_x”:[184,186,185,187,190,190,200,207,214,202,196,199,205,209,217,211,196,188,188] “all_points_y”:[314,320,336,337,335,322,331,343,345,327,319,309,301,293,286,287,307,316,317]}, “region_attributes”:{}},{ “shape_attributes”:{ “名称”: “多边形”, “all_points_x”:[86,84,90,93,104,122,118,101,107,119,132,136,126,103,90,96,97, 98,92,92,90,89],"all_points_y":[294,309,303,297,302,311,303,295,285,275,265,260,263,284,293,289,278,267,277,283,290,294]},"region_attributes":{}},{"shape_attributes":{"name":"polygon","all_points_x":[65,53,43,58,69,72,79,81,75,78,83,80, 72,70,71,66,64],"all_points_y":[150,177,184,180,151,151,155,151,146,142,115,99,137,138,128,118,126]},"region_attributes":{}},{"shape_attributes":{"name":"所有多边形点"," ,29,46,57,65,80,94,109,121,102,87,83,88,79,74,50,48],"all_points_y":[62,83,71,66,64,65,63,64,63 ,58,62,60,58,55,58,63,64]},"region_attributes":{}},{"shape_attributes":{"name":"polygon","all_points_x":[355,349,334,338,344,359,363,362,362,365,369,384,397,399,3] all_points_y":[131,136,142,143,142,137,135,143,153,145,136,133,131,127,130,132]},"region_attributes":{}},{"shape_attributes":{"name":"polygon","all_points_x":
查看完整描述

3 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

import json

data={}

with open('C:\\Users\\Takvaviya_PC_007\\Downloads\\via_region_data.json','r') as file:

    original = json.load(file)


for p in original:

    # print(p)

    # print(original[p])

    if original[p]['regions']!=[]:

        data[p]=original[p]

print(data)


with open('C:\\Users\\Takvaviya_PC_007\\Downloads\\pulse2.json', 'w') as outfile:

    json.dump(data, outfile)


查看完整回答
反对 回复 2022-01-18
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

你快到了,如果你尝试在每个文件中加载一个带有 json 对象的文件,你会得到一个解码错误,因为它不是有效的 json。


相反,您遍历每一行,然后加载 json 并在值为 时regions转储truthy。


在 python 中,空lists是空的,falsy因此将过滤掉具有空数组的区域。


import json


with open('filtered_data.json', 'w') as outfile:

    with open('all_data.json') as json_file:

        for line in json_file:

            data = json.loads(line)

            if data['regions']:

                outfile.write(json.dumps(data)+'\n')

all_data.json


{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group13918.jpg", ...other data...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14218.jpg", ...other data...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14219.jpg", ...other data...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14222.jpg", ...other data...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14243.jpg", ...other data...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14277.jpg", ...other data...}

过滤数据.json


{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group13918.jpg", ...other data ...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14218.jpg", ...other data ...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14219.jpg", ...other data ...}

{"filename": "SPI_121218_SITE2_NDVI_25m_transparent_mosaic_group14222.jpg", ...other data ...}



查看完整回答
反对 回复 2022-01-18
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

首先将 json 文件加载为字典,


import json

with open('<input>.json','r') as file:

    dct = json.load(file)

然后我们遍历字典并附加所有具有regions非空列表键的字典。


li = []

for k,v in dct.items():

    #Check if regions key exists, and the list is non-empty

    if 'regions' in v and v['regions']:

        li.append(v)

print(li)

然后我们将结果列表保存到另一个 json。



with open('<output>.json', 'w') as outfile:

    json.dump(li, outfile)


查看完整回答
反对 回复 2022-01-18
  • 3 回答
  • 0 关注
  • 222 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信