0

Consider the nested dictionary:

 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}]}

The first key "lassoPoints" is not important . I just would like to consider the Key="points" to have a list of "location" or a dataframe like:

 location
0 Cascais
1 Oeiras
2 Sintra

I tried to used Benedict:

Installation: pip install python-benedict

from benedict import benedict
mapSel= benedict(mapSel, keypath_separator='.')
val = mapSel.get('points.location')
val

Got nothing

Ricardo Gomes
  • 229
  • 1
  • 5

2 Answers2

1

Why don't just using 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

If you just need locations, then you can just access that particular column.

>>> df["location"]
0    Cascais
1     Oeiras
2     Sintra
Name: location, dtype: object
crissal
  • 2,547
  • 7
  • 25
1

Is this what you want?

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']
Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42