如何获取大词典中许多词典中的所有数据

问题描述 投票:0回答:2

我最近才进入API请求,在请求了一些信息后,我在许多词典中获取了很多数据,基本上是这样的,我想组织和仅打印下载URL,谢谢您的帮助。

这里是json的示例:

{
    'errorCode': None,
    'error': '',
    'data': {
        'numberReturned': 10,
        'totalHits': 388958,
        'firstRecord': 1,
        'lastRecord': 10,
        'nextRecord': 11,
        'results': [{
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-130.96631, 52.48409],
                        [-128.40618, 52.04284],
                        [-127.5665, 53.69859],
                        [-130.22375, 54.15318],
                        [-130.96631, 52.48409]
                    ]
                ]
            },
            'sceneBounds': '-130.96631,52.04284,-127.5665,54.15318',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/052/023/LT08_L1GT_052023_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80520232013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80520232013067LGN02/INVSVC/',
            'entityId': 'LT80520232013067LGN02',
            'displayId': 'LT08_L1GT_052023_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80520232013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80520232013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_052023_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 52, Row: 23'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-131.56161, 51.07001],
                        [-129.07552, 50.63805],
                        [-128.28062, 52.29816],
                        [-130.85695, 52.74239],
                        [-131.56161, 51.07001]
                    ]
                ]
            },
            'sceneBounds': '-131.56161,50.63805,-128.28062,52.74239',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/052/024/LT08_L1GT_052024_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80520242013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80520242013067LGN02/INVSVC/',
            'entityId': 'LT80520242013067LGN02',
            'displayId': 'LT08_L1GT_052024_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80520242013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80520242013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_052024_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 52, Row: 24'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-132.12903, 49.65182],
                        [-129.71182, 49.22835],
                        [-128.95719, 50.89234],
                        [-131.45832, 51.32713],
                        [-132.12903, 49.65182]
                    ]
                ]
            },
            'sceneBounds': '-132.12903,49.22835,-128.95719,51.32713',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/052/025/LT08_L1GT_052025_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80520252013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80520252013067LGN02/INVSVC/',
            'entityId': 'LT80520252013067LGN02',
            'displayId': 'LT08_L1GT_052025_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80520252013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80520252013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_052025_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 52, Row: 25'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-164.20358, 23.86964],
                        [-162.48625, 23.52277],
                        [-162.05927, 25.23064],
                        [-163.80043, 25.58141],
                        [-164.20358, 23.86964]
                    ]
                ]
            },
            'sceneBounds': '-164.20358,23.52277,-162.05927,25.58141',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/068/043/LT08_L1GT_068043_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80680432013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80680432013067LGN02/INVSVC/',
            'entityId': 'LT80680432013067LGN02',
            'displayId': 'LT08_L1GT_068043_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80680432013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80680432013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_068043_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 68, Row: 43'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-164.53819, 22.4285],
                        [-162.83937, 22.08332],
                        [-162.4197, 23.79243],
                        [-164.14054, 24.14135],
                        [-164.53819, 22.4285]
                    ]
                ]
            },
            'sceneBounds': '-164.53819,22.08332,-162.4197,24.14135',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/068/044/LT08_L1GT_068044_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80680442013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80680442013067LGN02/INVSVC/',
            'entityId': 'LT80680442013067LGN02',
            'displayId': 'LT08_L1GT_068044_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80680442013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80680442013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_068044_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 68, Row: 44'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-164.86886, 20.98582],
                        [-163.18712, 20.6422],
                        [-162.77413, 22.35249],
                        [-164.47616, 22.69974],
                        [-164.86886, 20.98582]
                    ]
                ]
            },
            'sceneBounds': '-164.86886,20.6422,-162.77413,22.69974',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/068/045/LT08_L1GT_068045_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80680452013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80680452013067LGN02/INVSVC/',
            'entityId': 'LT80680452013067LGN02',
            'displayId': 'LT08_L1GT_068045_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80680452013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80680452013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_068045_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 68, Row: 45'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [79.72568, 15.2114],
                        [81.3526, 14.87269],
                        [81.74391, 16.58657],
                        [80.103, 16.92848],
                        [79.72568, 15.2114]
                    ]
                ]
            },
            'sceneBounds': '79.72568,14.87269,81.74391,16.92848',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/142/049/LT08_L1GT_142049_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT81420492013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT81420492013067LGN02/INVSVC/',
            'entityId': 'LT81420492013067LGN02',
            'displayId': 'LT08_L1GT_142049_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT81420492013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT81420492013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_142049_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 142, Row: 49'
        }, {
            'acquisitionDate': '2013-03-08',
            'startTime': '2013-03-08',
            'endTime': '2013-03-08',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [79.41052, 13.76653],
                        [81.02677, 13.42875],
                        [81.41387, 15.1434],
                        [79.78508, 15.4843],
                        [79.41052, 13.76653]
                    ]
                ]
            },
            'sceneBounds': '79.41052,13.42875,81.41387,15.4843',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/142/050/LT08_L1GT_142050_20130308_20170505_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT81420502013067LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT81420502013067LGN02/INVSVC/',
            'entityId': 'LT81420502013067LGN02',
            'displayId': 'LT08_L1GT_142050_20130308_20170505_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT81420502013067LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT81420502013067LGN02/save_xml',
            'modifiedDate': '2017-05-05',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_142050_20130308_20170505_01_T2, Acquisition Date: 08-MAR-13, Path: 142, Row: 50'
        }, {
            'acquisitionDate': '2013-03-09',
            'startTime': '2013-03-09',
            'endTime': '2013-03-09',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-120.15339, 38.24051],
                        [-118.15904, 37.86641],
                        [-117.61337, 39.55749],
                        [-119.65581, 39.938],
                        [-120.15339, 38.24051]
                    ]
                ]
            },
            'sceneBounds': '-120.15339,37.86641,-117.61337,39.938',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/042/033/LT08_L1GT_042033_20130309_20170310_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80420332013068LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80420332013068LGN02/INVSVC/',
            'entityId': 'LT80420332013068LGN02',
            'displayId': 'LT08_L1GT_042033_20130309_20170310_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80420332013068LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80420332013068LGN02/save_xml',
            'modifiedDate': '2017-03-10',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_042033_20130309_20170310_01_T2, Acquisition Date: 09-MAR-13, Path: 42, Row: 33'
        }, {
            'acquisitionDate': '2013-03-09',
            'startTime': '2013-03-09',
            'endTime': '2013-03-09',
            'spatialFootprint': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-120.56102, 36.80643],
                        [-118.60521, 36.4365],
                        [-118.07638, 38.1302],
                        [-120.07712, 38.50615],
                        [-120.56102, 36.80643]
                    ]
                ]
            },
            'sceneBounds': '-120.56102,36.4365,-118.07638,38.50615',
            'browseUrl': 'https://ims.cr.usgs.gov/browse/landsat_8_c1/2013/042/034/LT08_L1GT_042034_20130309_20170310_01_T2.jpg',
            'dataAccessUrl': 'https://earthexplorer.usgs.gov/order/process?dataset_name=LANDSAT_8_C1&ordered=LT80420342013068LGN02&node=INVSVC',
            'downloadUrl': 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80420342013068LGN02/INVSVC/',
            'entityId': 'LT80420342013068LGN02',
            'displayId': 'LT08_L1GT_042034_20130309_20170310_01_T2',
            'cloudCover': None,
            'metadataUrl': 'https://earthexplorer.usgs.gov/metadata/xml/12864/LT80420342013068LGN02/',
            'fgdcMetadataUrl': 'https://earthexplorer.usgs.gov/fgdc/12864/LT80420342013068LGN02/save_xml',
            'modifiedDate': '2017-03-10',
            'orderUrl': None,
            'bulkOrdered': False,
            'ordered': False,
            'summary': 'Entity ID: LT08_L1GT_042034_20130309_20170310_01_T2, Acquisition Date: 09-MAR-13, Path: 42, Row: 34'
        }]
    },
    'api_version': '1.4.0',
    'access_level': 'user',
    'catalog_id': 'EE',
    'executionTime': 208.667964220047
}

如何获取大型词典中许多词典中的所有数据?

python dictionary
2个回答
0
投票

字典被嵌套在Python中,只需一次访问字典键:

data = { .. your big data file ...}
download_urls = [result['downloadUrl'] for result in data['data']['results']]
print(download_urls) # ['https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80520232013067LGN02/INVSVC/', 'https://earthexplorer.usgs.gov/download/external/options/LANDSAT_8_C1/LT80520242013067LGN02/INVSVC

0
投票

下面第一行之后,您将拥有一个包含所有数据的词典。我在其下面添加了代码,以打印出所有URL:

>>> exec(f"d = {open('/Users/todd/projects/landsat/landsat.json', 'r').read()}")
>>> 
>>> def print_urls(o, k=None):
...     ot = type(o)
...     if ot is list:
...         for li in o:
...             print_urls(li)
...     elif ot is dict:
...         for k, v in o.items():
...             print_urls(v, k)
...     elif ot is str and o.startswith("http"):
...         if k:
...             print(f"{k}: {o}")
...         else:
...             print(o)
...             
>>> print_urls(d)

如果您想使这本词典扁平化,我不知道是否可行,因为其中嵌套了许多同名的键。

© www.soinside.com 2019 - 2024. All rights reserved.