【发布时间】:2022-01-04 03:37:53
【问题描述】:
大家好, 我正在寻求帮助以尝试展平多个嵌套字典并将它们附加到新列表中。 我有多个字典,从这样的 geojson-File 加载:
data = json.load(open("xy.geojson"))
它们的结构都是这样的:
{'type': 'Feature', 'properties': {'tags': {'highway': 'cycleway', 'lit': 'yes', 'source': 'survey 08.2018 and Esri', 'surface': 'paving_stones', 'traffic_sign': 'DE:237 Radweg'}}, 'geometry': {'type': 'LineString', 'coordinates': [[6.7976974, 51.1935231], [6.7977131, 51.1935542], [6.7977735, 51.1935719], [6.7978679, 51.193578], [6.798005, 51.1936044], [6.7982118, 51.1936419], [6.7983474, 51.1936511], [6.7984899, 51.1936365], [6.7985761, 51.193623], [6.7986739, 51.1936186], [6.7987574, 51.1936188], [6.7988269, 51.1936342], [6.7988893, 51.1936529], [6.7989378, 51.1936778], [6.7990085, 51.1937739]]}}
现在我想展平字典的“标签”部分,所以我得到:
{'type': 'Feature', 'properties': {'highway': 'cycleway', 'lit': 'yes', 'source': 'survey 08.2018 and Esri', 'surface': 'paving_stones', 'traffic_sign': 'DE:237 Radweg'}, 'geometry': {'type': 'LineString', 'coordinates': [[6.7976974, 51.1935231], [6.7977131, 51.1935542], [6.7977735, 51.1935719], [6.7978679, 51.193578], [6.798005, 51.1936044], [6.7982118, 51.1936419], [6.7983474, 51.1936511], [6.7984899, 51.1936365], [6.7985761, 51.193623], [6.7986739, 51.1936186], [6.7987574, 51.1936188], [6.7988269, 51.1936342], [6.7988893, 51.1936529], [6.7989378, 51.1936778], [6.7990085, 51.1937739]]}}
到目前为止,我所做的是设置一个新列表并开始一个 for 循环:
filtered = []
for geo in data['features']:
但是如何在循环中展平 geo['properties']['tags'] 并将每个字典的结果附加到过滤中? 非常感谢大家,感谢大家的帮助! 克莱门斯
【问题讨论】:
标签: python json dictionary geojson flatten