从python中的dict列表中提取特定键。 Sentinelhub

时间:2018-03-24 23:10:07

标签: python

我似乎陷入了非常简单的任务。我仍然把我的脚趾浸入Python中。

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

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

System.out.println("x= "+ bb.x);
System.out.println("y= "+ bb.y);

您能解释一下我如何迭代这组数据并仅提取'productType'?例如,如果有几个类似的数据集,它将只返回不同的产品类型。

我的代码是:

{'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'}

4 个答案:

答案 0 :(得分:0)

根据您提供的内容,将底部替换为循环:

for i in data:
  print(i)

以下内容:

for i in data:
  print(i['properties']['**productType**'])

答案 1 :(得分:0)

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

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

所以你可以在循环中使用它(你的data_array是数据,由sentinelhub api返回):

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

答案 2 :(得分:0)

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)

答案 3 :(得分:0)

  1. 仅获取特定(嵌套)值:由于您的请求键是嵌套的,并且位于父“属性”对象内,因此您需要首先访问它,最好使用get方法。这可以按如下方式完成(注意第一个get中的'{}'参数,如果第一个键不存在,则返回一个空字典)
  2. data_dictionary = json.loads(data_string)
    product_type = data_dictionary.get('properties', {}).get('**productType**')
    
    1. 然后,您可以在set中聚合不同的product_type对象,这将自动保证没有2个对象相同
    2. product_type_set = set()
      product_type.add(product_type)