Python: Read information from .yaml
up vote
0
down vote
favorite
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!
yaml python-3.7 pyyaml
add a comment |
up vote
0
down vote
favorite
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!
yaml python-3.7 pyyaml
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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!
yaml python-3.7 pyyaml
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!
yaml python-3.7 pyyaml
yaml python-3.7 pyyaml
asked Nov 21 at 13:29
Vroni
214
214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
edited Nov 21 at 14:20
Anthon
27.8k1691142
27.8k1691142
answered Nov 21 at 13:40
Vroni
214
214
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413128%2fpython-read-information-from-yaml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown