I set up a .yaml file that contains meta data for my measurement points. In this .yaml file I used nested lists and dictionaries that contain the informations, e.g.:
stations:
- XXXX:
statnr: 11111
name: NAME
name_csv: CSV
name_snowpack: NAME_SHORT
lat: 11.11111
lon: 11.22222
alt: 1111
type: TYPE
operator: OPERATOR
param:
- x1
- x2
- x3
- x4
- x5
- YYYY:
statnr: 22222
name: NAME2
name_csv: CSV2
name_snowpack: NAME_SHORT2
lat: 22.22222
lon: 22.33333
alt: 2222
type: TYPE2
operator: OPERATOR2
param:
- y1
- y2
- y3
- y4
- y5
Next I tried to read specific entries from that file.
import yaml
with open('./config/stations.yaml','r') as file:
meta = yaml.load(file)
stations = meta['stations']
print(stations[0])
This works and prints out all information about list entry 'XXXX' but if I want to only retrieve the information about the operator like I would do with a python dictionary:
print(stations[0]['operator'])
I get a: KeyError:'operator'.
So how can I address this entry or maybe entries even one level below that? Thanks for helping!
Found the answer to the question myself. Obviously didn't try that thoroughly enough before..
Instead of having a list of stations in my stations.yaml like above:
stations:
- XXXX:
statnr:1111
....
- YYYY:
statnr:2222
....
I use another dictionary:
stations:
XXXX:
statnr:1111
param:
-x1
-x2
....
YYYY:
statnr:2222
param:
-x1
-x2
....
In this way I can use:
import yaml
with open('./config/stations.yaml','r') as file:
meta = yaml.load(file)
stations = meta['stations']
txt = stations['XXXX']['param'][0]
print(txt)
and get the result
x1
which is exactly what I was looking for.