考虑嵌套字典:
mapSel={'lassoPoints': {'mapbox': [[-9.51, 38.96], [-9.28, 38.78],
[-9.24, 38.78], [-9.22, 38.70], [-9.29, 38.68], [-9.25,
38.70], [-9.32, 38.69], [-9.38, 38.60]]}, 'points': [{'curveNumber': 0,
'location': 'Cascais', 'pointIndex': 152, 'pointNumber': 152,
'z': 187.769}, {'curveNumber': 0, 'location': 'Oeiras',
'pointIndex': 158, 'pointNumber': 158, 'z': 186.113},
{'curveNumber': 0, 'location': 'Sintra', 'pointIndex': 159,
'pointNumber': 159, 'z': 221.223}]}
第一个键“套索点”并不重要。我只想考虑 Key="points" 有一个“位置”列表或一个数据框,如:
location
0 Cascais
1 Oeiras
2 Sintra
我尝试使用本尼迪克特:
安装:pip install python-benedict
from benedict import benedict
mapSel= benedict(mapSel, keypath_separator='.')
val = mapSel.get('points.location')
val
什么都没有
答案 0 :(得分:1)
为什么不只使用 pandas
?
>>> import pandas as pd
>>> df = pd.DataFrame(mapSel["points"])
>>> df
curveNumber location pointIndex pointNumber z
0 0 Cascais 152 152 187.769
1 0 Oeiras 158 158 186.113
2 0 Sintra 159 159 221.223
如果您只需要 location
,那么您只需访问该特定列即可。
>>> df["location"]
0 Cascais
1 Oeiras
2 Sintra
Name: location, dtype: object
答案 1 :(得分:1)
这是你想要的吗?
mapSel={'lassoPoints': {'mapbox': [[-9.51, 38.96], [-9.28, 38.78],
[-9.24, 38.78], [-9.22, 38.70], [-9.29, 38.68], [-9.25,
38.70], [-9.32, 38.69], [-9.38, 38.60]]},
'points': [{'curveNumber': 0,
'location': 'Cascais', 'pointIndex': 152, 'pointNumber': 152,
'z': 187.769}, {'curveNumber': 0, 'location': 'Oeiras',
'pointIndex': 158, 'pointNumber': 158, 'z': 186.113},
{'curveNumber': 0, 'location': 'Sintra', 'pointIndex': 159,
'pointNumber': 159, 'z': 221.223}]}
print([elt['location'] for elt in mapSel['points']])
# >>> ['Cascais', 'Oeiras', 'Sintra']