在python中从dict列表中提取特定键。哨兵俱乐部

2024-06-28 11:10:49 发布

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

我似乎被困在一个非常简单的任务上。我还在把脚趾伸进Python身上。你知道吗

我正在尝试使用SentinelHub API下载Sentinel 2图像:SentinelHub

我的代码返回的数据结果如下:

{'geometry': {'coordinates': [[[[35.895906644, 31.602691754],
     [36.264307655, 31.593801516],
     [36.230618703, 30.604681346],
     [35.642363693, 30.617971909],
     [35.678587829, 30.757888786],
     [35.715700562, 30.905919341],
     [35.754290061, 31.053632806],
     [35.793289298, 31.206946419],
     [35.895906644, 31.602691754]]]],
  'type': 'MultiPolygon'},
 'id': 'ee923fac-0097-58a8-b861-b07d89b99310',
 'properties': {'**productType**': '**S2MSI1C**',
  'centroid': {'coordinates': [18.1321538275, 31.10368655], 'type': 'Point'},
  'cloudCover': 10.68,
  'collection': 'Sentinel2',
  'completionDate': '2017-06-07T08:15:54Z',
  'description': None,
  'instrument': 'MSI',
  'keywords': [],
  'license': {'description': {'shortName': 'No license'},
   'grantedCountries': None,
   'grantedFlags': None,
   'grantedOrganizationCountries': None,
   'hasToBeSigned': 'never',
   'licenseId': 'unlicensed',
   'signatureQuota': -1,
   'viewService': 'public'},
  'links': [{'href': 'http://opensearch.sentinel-hub.com/resto/collections/Sentinel2/ee923fac-0097-58a8-b861-b07d89b99310.json?&lang=en',
    'rel': 'self',
    'title': 'GeoJSON link for ee923fac-0097-58a8-b861-b07d89b99310',
    'type': 'application/json'}],
  'orbitNumber': 10228,
  'organisationName': None,
  'parentIdentifier': None,
  'platform': 'Sentinel-2',
  'processingLevel': '1C',
  'productIdentifier': 'S2A_OPER_MSI_L1C_TL_SGS__20170607T120016_A010228_T36RYV_N02.05',
  'published': '2017-07-26T13:09:17.405352Z',
  'quicklook': None,
  'resolution': 10,
  's3Path': 'tiles/36/R/YV/2017/6/7/0',
  's3URI': 's3://sentinel-s2-l1c/tiles/36/R/YV/2017/6/7/0/',
  'sensorMode': None,
  'services': {'download': {'mimeType': 'text/html',
    'url': 'http://sentinel-s2-l1c.s3-website.eu-central-1.amazonaws.com#tiles/36/R/YV/2017/6/7/0/'}},
  'sgsId': 2168915,
  'snowCover': 0,
  'spacecraft': 'S2A',
  'startDate': '2017-06-07T08:15:54Z',
  'thumbnail': None,
  'title': 'S2A_OPER_MSI_L1C_TL_SGS__20170607T120016_A010228_T36RYV_N02.05',
  'updated': '2017-07-26T13:09:17.405352Z'},
 'type': 'Feature'}

你能解释一下我如何遍历这组数据并只提取“productType”吗?例如,如果有几个相似的数据集,它将只返回不同的产品类型。你知道吗

我的代码是:

 import matplotlib.pyplot as plt
    import numpy as np
    from sentinelhub import AwsProductRequest, AwsTileRequest, AwsTile, BBox, CRS
    betsiboka_coords_wgs84 = [31.245117,33.897777,34.936523,36.129002]
    bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
    date= '2017-06-05',('2017-06-08')
    data=sentinelhub.opensearch.get_area_info(bbox, date_interval=date, maxcc=None)
for i in data:
    print(i)

Tags: 数据importnonedatetypesentinelmsitiles
3条回答

根据您提供的内容,更换您的底部for循环:

for i in data:
  print(i)

包括以下内容:

for i in data:
  print(i['properties']['**productType**'])
keys = []  
for key in d.keys():
     if key == 'properties':
         for k in d[key].keys():
            if k == '**productType**' and k not in keys:         
            keys.append(d[key][k])
print(keys)

如果只想访问propertyType,可以在for循环中使用i['properties']['productType']。如果您想在任何时候访问它,而不必每次写入这些键,您可以定义一个生成器,如下所示:

def property_types(data_array):
    for data in data_array
        yield data['properties']['propertyType']

所以您可以像这样在循环中使用它(您的数据数组是由sentinelhub api返回的数据):

for property_type in property_types(data):
    # do stuff with property_type

相关问题 更多 >