循环以提取JSON中字典中的某些部分

2024-05-16 09:39:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有1500个JSON格式的元素,但我需要用python编写一个字典。每个元素都与一个ID关联(ID也在字典中,是多边形表示)

这些元素具有相同的结构:

OrderedDict([('d5b6d112-903b-4648-833e-d7c3fdac490b',
              {'title': 'S2A_MSIL1C_20200410T105031_N0209_R051_T31TCF_20200410T112156',
               'link': "https://scihub.copernicus.eu/dhus/odata/v1/Products('d5b6d112-903b-4648-833e-d7c3fdac490b')/$value",
               'link_alternative': "https://scihub.copernicus.eu/dhus/odata/v1/Products('d5b6d112-903b-4648-833e-d7c3fdac490b')/",
               'link_icon': "https://scihub.copernicus.eu/dhus/odata/v1/Products('d5b6d112-903b-4648-833e-d7c3fdac490b')/Products('Quicklook')/$value",
               'summary': 'Date: 2020-04-10T10:50:31.024Z, Instrument: MSI, Mode: , Satellite: Sentinel-2, Size: 708.56 MB',
               'datatakesensingstart': datetime.datetime(2020, 4, 10, 10, 50, 31, 24000),
               'beginposition': datetime.datetime(2020, 4, 10, 10, 50, 31, 24000),
               'endposition': datetime.datetime(2020, 4, 10, 10, 50, 31, 24000),
               'ingestiondate': datetime.datetime(2020, 4, 10, 15, 23, 51, 463000),
               'orbitnumber': 25073,
               'relativeorbitnumber': 51,
               'cloudcoverpercentage': 25.1653,
               'sensoroperationalmode': 'INS-NOBS',
               'gmlfootprint': '<gml:Polygon srsName="http://www.opengis.net/gml/srs/epsg.xml#4326" xmlns:gml="http://www.opengis.net/gml">\n   <gml:outerBoundaryIs>\n      <gml:LinearRing>\n         <gml:coordinates>41.12630256662179,1.925343307665624 41.0388866778506,1.893218517758493 40.89259720303277,1.839655652731482 40.74637877387899,1.786307008717412 40.60020137146172,1.733323787214736 40.554608185948275,1.716936587906194 40.538607851727775,0.638419022640916 41.52685230149302,0.6028173807795 41.546758769895376,1.918508192616414 41.12630256662179,1.925343307665624</gml:coordinates>\n      </gml:LinearRing>\n   </gml:outerBoundaryIs>\n</gml:Polygon>',
               'footprint': 'MULTIPOLYGON (((0.638419022640916 40.538607851727775, 1.716936587906194 40.554608185948275, 1.733323787214736 40.60020137146172, 1.786307008717412 40.74637877387899, 1.839655652731482 40.89259720303277, 1.893218517758493 41.0388866778506, 1.925343307665624 41.12630256662179, 1.918508192616414 41.546758769895376, 0.6028173807795 41.52685230149302, 0.638419022640916 40.538607851727775)))',

我需要摘录:标题、示意图和datatakesensingstart。我希望所有提取的元素都存储在一个列表中

非常感谢你


Tags: https元素datetime字典linkproductsv1eu
1条回答
网友
1楼 · 发布于 2024-05-16 09:39:13

给定一个OrderedDict对象列表,按照您提到的结构,您可以使用列表理解来创建一个只包含您感兴趣的键的精简字典列表

trimmed_data = [
    {
        'Title': element[1]['Title'],
        'footprint': element[1]['footprint'],
        'datatakesensingstart': element[1]['datatakesensingstart'],
    }
    for element in data
]

相关问题 更多 >