Python: Read information from .yaml
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 |
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 |
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 '18 at 13:29
VroniVroni
435
435
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 14:20
Anthon
31.4k1796150
31.4k1796150
answered Nov 21 '18 at 13:40
VroniVroni
435
435
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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