使用 FOR 循環遍歷 JSON 對象

瑞安·克萊默

我目前正在參加數據分析新兵訓練營,並且在 6 週內收到了大量信息。在 python 和 juypter 筆記本中工作。我在 JSON 對像上運行 FOR 循環時遇到了一些問題。特別希望從json中的每個不同的“padid”中提取6個“full_name”,並按順序打印它們。我似乎無法讓我可憐的疲倦的大腦圍繞我需要讓它運行的語法。任何建議或見解都會非常有幫助!

import requests
import json

url = "https://api.spacexdata.com/v2/launchpads"

space = requests.get(url)

spacex= space.json()
spacex

[{'padid': 5,
  'id': 'vafb_slc_3w',
  'name': 'VAFB SLC 3W',
  'full_name': 'Vandenberg Air Force Base Space Launch Complex 3W',
  'status': 'retired',
  'location': {'name': 'Vandenberg Air Force Base',
   'region': 'California',
   'latitude': 34.6440904,
   'longitude': -120.5931438},
  'vehicles_launched': ['Falcon 1'],
  'attempted_launches': 0,
  'successful_launches': 0,
  'wikipedia': 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_3',
  'details': 'SpaceX original west coast launch pad for Falcon 1. Performed a static fire but was never used for a launch and abandoned due to scheduling conflicts.'},
 {'padid': 2,
  'id': 'ccafs_slc_40',
  'name': 'CCAFS SLC 40',
  'full_name': 'Cape Canaveral Air Force Station Space Launch Complex 40',
  'status': 'active',
  'location': {'name': 'Cape Canaveral',
   'region': 'Florida',
   'latitude': 28.5618571,
   'longitude': -80.577366},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 61,
  'successful_launches': 59,
  'wikipedia': 'https://en.wikipedia.org/wiki/Cape_Canaveral_Air_Force_Station_Space_Launch_Complex_40',
  'details': 'SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15.'},
 {'padid': 8,
  'id': 'stls',
  'name': 'STLS',
  'full_name': 'SpaceX South Texas Launch Site',
  'status': 'under construction',
  'location': {'name': 'Boca Chica Village',
   'region': 'Texas',
   'latitude': 25.9972641,
   'longitude': -97.1560845},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 0,
  'successful_launches': 0,
  'wikipedia': 'https://en.wikipedia.org/wiki/SpaceX_South_Texas_Launch_Site',
  'details': 'SpaceX new launch site currently under construction to help keep up with the Falcon 9 and Heavy manifests. Expected to be completed in late 2018. Initially will be limited to 12 flights per year, and only GTO launches.'},
 {'padid': 1,
  'id': 'kwajalein_atoll',
  'name': 'Kwajalein Atoll',
  'full_name': 'Kwajalein Atoll Omelek Island',
  'status': 'retired',
  'location': {'name': 'Omelek Island',
   'region': 'Marshall Islands',
   'latitude': 9.0477206,
   'longitude': 167.7431292},
  'vehicles_launched': ['Falcon 1'],
  'attempted_launches': 5,
  'successful_launches': 2,
  'wikipedia': 'https://en.wikipedia.org/wiki/Omelek_Island',
  'details': 'SpaceX original launch site, where all of the Falcon 1 launches occured. Abandoned as SpaceX decided against upgrading the pad to support Falcon 9.'},
 {'padid': 6,
  'id': 'vafb_slc_4e',
  'name': 'VAFB SLC 4E',
  'full_name': 'Vandenberg Air Force Base Space Launch Complex 4E',
  'status': 'active',
  'location': {'name': 'Vandenberg Air Force Base',
   'region': 'California',
   'latitude': 34.632093,
   'longitude': -120.610829},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 16,
  'successful_launches': 16,
  'wikipedia': 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4',
  'details': 'SpaceX primary west coast launch pad for polar orbits and sun synchronous orbits, primarily used for Iridium. Also intended to be capable of launching Falcon Heavy.'},
 {'padid': 4,
  'id': 'ksc_lc_39a',
  'name': 'KSC LC 39A',
  'full_name': 'Kennedy Space Center Historic Launch Complex 39A',
  'status': 'active',
  'location': {'name': 'Cape Canaveral',
   'region': 'Florida',
   'latitude': 28.6080585,
   'longitude': -80.6039558},
  'vehicles_launched': ['Falcon 9', 'Falcon Heavy'],
  'attempted_launches': 27,
  'successful_launches': 26,
  'wikipedia': 'https://en.wikipedia.org/wiki/Kennedy_Space_Center_Launch_Complex_39#Launch_Complex_39A',
  'details': 'NASA historic launch pad that launched most of the Saturn V and Space Shuttle missions. Initially for Falcon Heavy launches, it is now launching all of SpaceX east coast missions due to the damage from the AMOS-6 anomaly. After SLC-40 repairs are complete, it will be upgraded to support Falcon Heavy, a process which will take about two months. In the future it will launch commercial crew missions and the Interplanetary Transport System.'}]
巴馬

spacex只是一個字典列表。您可以像任何其他列表一樣遍歷它。

for site in spacex:
    print(site['full_name'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章