在Python中从嵌套字典中提取值



我正在寻找一些关于如何处理JSON/字典数据的建议,我从API拉。我试图使用的API是FlightRadar24(流行的航班跟踪服务)的Python实现。我试图完成的是获取它当前跟踪的每个航班的信息,即呼号、飞机类型、高度、目的地等。

这是通过查询特定航空公司的所有航班来实现的,如下所示:

import flightradar24
fr24 = flightradar24.Api()
delta_flights = fr24.get_flights("ICE")

上面这个简单的调用将返回冰岛航空当前活动的所有航班的所有信息。现在我可以不断地循环调用每个航空公司来获取当前所有航班的字典。然而,它返回航班的结构让我感到困惑。下面是仅调用ICE时返回的字典结构的示例。

{'full_count': 15510, 'version': 4, '289610b7': ['4CC2AD', 41.0623, -4.4359, 125, 38000, 499, '1161', 'F-LERL4', 'B752', 'TF-FIV', 1627561427, 'KEF', 'ALC', 'FI1582', 0, 0, 'ICE1582', 0, 'ICE'], '28962687': ['4CC52C', 56.2083, -57.1494, 219, 38000, 430, '0000', 'T-F5M', 'B38M', 'TF-ICN', 1627561428, 'KEF', 'BOS', 'FI635', 0, 0, 'ICE635', 0, 'ICE'], '28963993': ['4CC330', 67.8567, -39.1096, 293, 23975, 281, '2772', 'F-BGKK1', 'DH8B', 'TF-FXG', 1627561420, 'RKV', '', 'FI1021', 0, -64, 'ICE1021', 0, 'ICE'], '2896784d': ['4CC2A6', 60.4406, 8.6048, 294, 34975, 490, '0244', 'F-ENRY3', 'B752', 'TF-FIO', 1627561421, 'OSL', 'KEF', 'FI319', 0, 2560, 'ICE319', 0, 'ICE'], '28967c03': ['4CC52B', 47.4731, 7.002, 290, 23800, 434, '3053', 'F-LSZH2', 'B39M', 'TF-ICA', 1627561427, 'ZRH', 'KEF', 'FI569', 0, 1600, 'ICE9L', 0, 'ICE'], '28967d8f': ['4CC50E', 60.3222, 15.8041, 300, 27550, 448, '0263', 'F-ESOW6', 'B38M', 'TF-ICE', 1627561426, 'ARN', 'KEF', 'FI307', 0, 1536, 'ICE307', 0, 'ICE'], '28967e4c': ['4CC50F', 51.0326, 3.9563, 292, 11625, 294, '4421', 'F-EHRD5', 'B38M', 'TF-ICY', 1627561427, 'BRU', 'KEF', 'FI555', 0, 2688, 'ICE67K', 0, 'ICE'], '28968886': ['4CC27E', 52.3049, 4.774, 140, 0, 5, '2113', 'F-EHAM1', 'B763', 'TF-ISP', 1627561427, 'AMS', 'KEF', 'FI501', 1, 0, 'ICE501', 0, 'ICE'], '2896894c': ['4CC279', 51.4691, -0.4405, 180, 0, 1, '6311', 'F-EGLL2', 'B763', 'TF-ISO', 1627561346, 'LHR', 'KEF', 'FI451', 1, 0, 'ICE451', 0, 'ICE'], '28968b37': ['4CC27F', 50.0507, 8.5915, 253, 0, 0, '2555', 'F-EDDF6', 'B763', 'TF-ISW', 1627561427, 'FRA', 'KEF', 'FI521', 1, 0, 'ICE521', 0, 'ICE'], '28968baf': ['4CC27C', 55.6256, 12.6421, 64, 0, 5, '0430', 'F-EKCH2', 'B752', 'TF-ISR', 1627561425, 'CPH', 'KEF', 'FI207', 1, 0, 'ICE93R', 0, 'ICE'], '28968be0': ['4CC331', 64.1305, -21.9461, 115, 0, 13, '4111', 'F-BIRK3', 'DH8B', 'TF-FXK', 1627561427, 'RKV', 'GOH', 'FI111', 1, 0, 'ICE111', 0, 'ICE'], 'stats': {'total': {'ads-b': 12468, 'mlat': 796, 'faa': 297, 'flarm': 1478, 'estimated': 193, 'satellite': 171, 'other': 0}, 'visible': {'ads-b': 11, 'mlat': 0, 'faa': 1, 'flarm': 0, 'estimated': 0, 'satellite': 0, 'other': 0}}}

到目前为止我发现的是,我们在这里有一个字典,每个航班有一个唯一的键,然后那个航班的字典值在一个列表中。在该列表中是我想提取的信息,如飞机类型、高度、位置、目的地等。因此,简单地打印上述字典的键,我们得到:

dict_keys(['full_count', 'version', '289610b7', '28962687', '28963993', '2896784d', '28967c03', '28967d8f', '28967e4c', '28968886', '28968b37', '28968baf', '28968be0', 'stats'])

对于键,full_count和version是FR24跟踪的航班总数,FR24版本,然后stats是我们查询的所有航班的跟踪方式的细分,因此在11个键(航班)中,所有11个都使用ADS-B进行跟踪。键值本身是没有意义的,只是一个唯一的标识符,我真正想要的是键保存的值。

我想做的是从这个字典的值中提取信息。所以我要找一份每个航班的详细清单,上面有呼号、高度、目的地等。但是,我不确定如何循环遍历这个字典以正确提取所有值。

我希望这是一个清楚的解释,感谢任何帮助。

字典中的值是list。你可以像处理其他列表一样循环遍历它们。

flights = {'full_count': 15510, 'version': 4, '289610b7': ['4CC2AD', 41.0623, -4.4359, 125, 38000, 499, '1161', 'F-LERL4', 'B752', 'TF-FIV', 1627561427, 'KEF', 'ALC', 'FI1582', 0, 0, 'ICE1582', 0, 'ICE'], '28962687': ['4CC52C', 56.2083, -57.1494, 219, 38000, 430, '0000', 'T-F5M', 'B38M', 'TF-ICN', 1627561428, 'KEF', 'BOS', 'FI635', 0, 0, 'ICE635', 0, 'ICE'], '28963993': ['4CC330', 67.8567, -39.1096, 293, 23975, 281, '2772', 'F-BGKK1', 'DH8B', 'TF-FXG', 1627561420, 'RKV', '', 'FI1021', 0, -64, 'ICE1021', 0, 'ICE'], '2896784d': ['4CC2A6', 60.4406, 8.6048, 294, 34975, 490, '0244', 'F-ENRY3', 'B752', 'TF-FIO', 1627561421, 'OSL', 'KEF', 'FI319', 0, 2560, 'ICE319', 0, 'ICE'], '28967c03': ['4CC52B', 47.4731, 7.002, 290, 23800, 434, '3053', 'F-LSZH2', 'B39M', 'TF-ICA', 1627561427, 'ZRH', 'KEF', 'FI569', 0, 1600, 'ICE9L', 0, 'ICE'], '28967d8f': ['4CC50E', 60.3222, 15.8041, 300, 27550, 448, '0263', 'F-ESOW6', 'B38M', 'TF-ICE', 1627561426, 'ARN', 'KEF', 'FI307', 0, 1536, 'ICE307', 0, 'ICE'], '28967e4c': ['4CC50F', 51.0326, 3.9563, 292, 11625, 294, '4421', 'F-EHRD5', 'B38M', 'TF-ICY', 1627561427, 'BRU', 'KEF', 'FI555', 0, 2688, 'ICE67K', 0, 'ICE'], '28968886': ['4CC27E', 52.3049, 4.774, 140, 0, 5, '2113', 'F-EHAM1', 'B763', 'TF-ISP', 1627561427, 'AMS', 'KEF', 'FI501', 1, 0, 'ICE501', 0, 'ICE'], '2896894c': ['4CC279', 51.4691, -0.4405, 180, 0, 1, '6311', 'F-EGLL2', 'B763', 'TF-ISO', 1627561346, 'LHR', 'KEF', 'FI451', 1, 0, 'ICE451', 0, 'ICE'], '28968b37': ['4CC27F', 50.0507, 8.5915, 253, 0, 0, '2555', 'F-EDDF6', 'B763', 'TF-ISW', 1627561427, 'FRA', 'KEF', 'FI521', 1, 0, 'ICE521', 0, 'ICE'], '28968baf': ['4CC27C', 55.6256, 12.6421, 64, 0, 5, '0430', 'F-EKCH2', 'B752', 'TF-ISR', 1627561425, 'CPH', 'KEF', 'FI207', 1, 0, 'ICE93R', 0, 'ICE'], '28968be0': ['4CC331', 64.1305, -21.9461, 115, 0, 13, '4111', 'F-BIRK3', 'DH8B', 'TF-FXK', 1627561427, 'RKV', 'GOH', 'FI111', 1, 0, 'ICE111', 0, 'ICE'], 'stats': {'total': {'ads-b': 12468, 'mlat': 796, 'faa': 297, 'flarm': 1478, 'estimated': 193, 'satellite': 171, 'other': 0}, 'visible': {'ads-b': 11, 'mlat': 0, 'faa': 1, 'flarm': 0, 'estimated': 0, 'satellite': 0, 'other': 0}}}
for key in flights:
if isinstance(flights[key], list):
for val in flights[key]:
print(val) ## do anything

使用isinstance确认值为列表数据类型,以便我们可以继续迭代。(字典中的一些值是整型数据类型,这将处理)。

您可以遍历键并尝试将它们转换为int。如果成功,取数组的第一个元素:

flight_dict = {'full_count': 15510, 'version': 4, '289610b7': ['4CC2AD', 41.0623, -4.4359, 125, 38000, 499, '1161', 'F-LERL4', 'B752', 'TF-FIV', 1627561427, 'KEF', 'ALC', 'FI1582', 0, 0, 'ICE1582', 0, 'ICE'], '28962687': ['4CC52C', 56.2083, -57.1494, 219, 38000, 430, '0000', 'T-F5M', 'B38M', 'TF-ICN', 1627561428, 'KEF', 'BOS', 'FI635', 0, 0, 'ICE635', 0, 'ICE'], '28963993': ['4CC330', 67.8567, -39.1096, 293, 23975, 281, '2772', 'F-BGKK1', 'DH8B', 'TF-FXG', 1627561420, 'RKV', '', 'FI1021', 0, -64, 'ICE1021', 0, 'ICE'], '2896784d': ['4CC2A6', 60.4406, 8.6048, 294, 34975, 490, '0244', 'F-ENRY3', 'B752', 'TF-FIO', 1627561421, 'OSL', 'KEF', 'FI319', 0, 2560, 'ICE319', 0, 'ICE'], '28967c03': ['4CC52B', 47.4731, 7.002, 290, 23800, 434, '3053', 'F-LSZH2', 'B39M', 'TF-ICA', 1627561427, 'ZRH', 'KEF', 'FI569', 0, 1600, 'ICE9L', 0, 'ICE'], '28967d8f': ['4CC50E', 60.3222, 15.8041, 300, 27550, 448, '0263', 'F-ESOW6', 'B38M', 'TF-ICE', 1627561426, 'ARN', 'KEF', 'FI307', 0, 1536, 'ICE307', 0, 'ICE'], '28967e4c': ['4CC50F', 51.0326, 3.9563, 292, 11625, 294, '4421', 'F-EHRD5', 'B38M', 'TF-ICY', 1627561427, 'BRU', 'KEF', 'FI555', 0, 2688, 'ICE67K', 0, 'ICE'], '28968886': ['4CC27E', 52.3049, 4.774, 140, 0, 5, '2113', 'F-EHAM1', 'B763', 'TF-ISP', 1627561427, 'AMS', 'KEF', 'FI501', 1, 0, 'ICE501', 0, 'ICE'], '2896894c': ['4CC279', 51.4691, -0.4405, 180, 0, 1, '6311', 'F-EGLL2', 'B763', 'TF-ISO', 1627561346, 'LHR', 'KEF', 'FI451', 1, 0, 'ICE451', 0, 'ICE'], '28968b37': ['4CC27F', 50.0507, 8.5915, 253, 0, 0, '2555', 'F-EDDF6', 'B763', 'TF-ISW', 1627561427, 'FRA', 'KEF', 'FI521', 1, 0, 'ICE521', 0, 'ICE'], '28968baf': ['4CC27C', 55.6256, 12.6421, 64, 0, 5, '0430', 'F-EKCH2', 'B752', 'TF-ISR', 1627561425, 'CPH', 'KEF', 'FI207', 1, 0, 'ICE93R', 0, 'ICE'], '28968be0': ['4CC331', 64.1305, -21.9461, 115, 0, 13, '4111', 'F-BIRK3', 'DH8B', 'TF-FXK', 1627561427, 'RKV', 'GOH', 'FI111', 1, 0, 'ICE111', 0, 'ICE'], 'stats': {'total': {'ads-b': 12468, 'mlat': 796, 'faa': 297, 'flarm': 1478, 'estimated': 193, 'satellite': 171, 'other': 0}, 'visible': {'ads-b': 11, 'mlat': 0, 'faa': 1, 'flarm': 0, 'estimated': 0, 'satellite': 0, 'other': 0}}}
for key in flight_dict:
try:
key_hex = bytes.fromhex(key)
print(flight_dict[key])
except Exception:
pass

编辑:我可能有点误解了这个问题,其他值可以通过适当的索引或通过flight_dict[key]循环访问

EDIT2:我还监督了飞行参考十六进制,而不是int (:facepalm:)。我删除了索引,以便打印整个数组:-)

相关内容

  • 没有找到相关文章