How to print the first key by searching from nested yaml using python
I have below nested yaml file, I want extract only NN41_R11
.
devices:
NN41_R11:
connections:
defaults:
a:
ip:
port:
protocol: telnet
class:
type: IOS
testbed:
name:
I am new to yaml parsing using python, below is the code which i tried to pseudo code, but its printing the entire yaml file.
import yaml
stream = open('/tmp/testbed1.yaml','r')
data = yaml.load(stream)
print data.get('devices')
python-3.x python-2.7 parsing yaml
add a comment |
I have below nested yaml file, I want extract only NN41_R11
.
devices:
NN41_R11:
connections:
defaults:
a:
ip:
port:
protocol: telnet
class:
type: IOS
testbed:
name:
I am new to yaml parsing using python, below is the code which i tried to pseudo code, but its printing the entire yaml file.
import yaml
stream = open('/tmp/testbed1.yaml','r')
data = yaml.load(stream)
print data.get('devices')
python-3.x python-2.7 parsing yaml
but its printing the entire yaml file.
- It prints a subtree corresponded to "devices" node. If you want to get the first key of that subtree, uselist(subtree.keys())[0]
or any other receipt from that question: stackoverflow.com/questions/30362391/…. Note, that YAML standard doesn't define order of the keys, so the first key extracted from subtree needn't to be the first key in the yaml file. However, there are some ways for remain that order: stackoverflow.com/questions/13297744/…
– Tsyvarev
Feb 22 '18 at 15:57
add a comment |
I have below nested yaml file, I want extract only NN41_R11
.
devices:
NN41_R11:
connections:
defaults:
a:
ip:
port:
protocol: telnet
class:
type: IOS
testbed:
name:
I am new to yaml parsing using python, below is the code which i tried to pseudo code, but its printing the entire yaml file.
import yaml
stream = open('/tmp/testbed1.yaml','r')
data = yaml.load(stream)
print data.get('devices')
python-3.x python-2.7 parsing yaml
I have below nested yaml file, I want extract only NN41_R11
.
devices:
NN41_R11:
connections:
defaults:
a:
ip:
port:
protocol: telnet
class:
type: IOS
testbed:
name:
I am new to yaml parsing using python, below is the code which i tried to pseudo code, but its printing the entire yaml file.
import yaml
stream = open('/tmp/testbed1.yaml','r')
data = yaml.load(stream)
print data.get('devices')
python-3.x python-2.7 parsing yaml
python-3.x python-2.7 parsing yaml
asked Feb 22 '18 at 15:46
VadirajVadiraj
204
204
but its printing the entire yaml file.
- It prints a subtree corresponded to "devices" node. If you want to get the first key of that subtree, uselist(subtree.keys())[0]
or any other receipt from that question: stackoverflow.com/questions/30362391/…. Note, that YAML standard doesn't define order of the keys, so the first key extracted from subtree needn't to be the first key in the yaml file. However, there are some ways for remain that order: stackoverflow.com/questions/13297744/…
– Tsyvarev
Feb 22 '18 at 15:57
add a comment |
but its printing the entire yaml file.
- It prints a subtree corresponded to "devices" node. If you want to get the first key of that subtree, uselist(subtree.keys())[0]
or any other receipt from that question: stackoverflow.com/questions/30362391/…. Note, that YAML standard doesn't define order of the keys, so the first key extracted from subtree needn't to be the first key in the yaml file. However, there are some ways for remain that order: stackoverflow.com/questions/13297744/…
– Tsyvarev
Feb 22 '18 at 15:57
but its printing the entire yaml file.
- It prints a subtree corresponded to "devices" node. If you want to get the first key of that subtree, use list(subtree.keys())[0]
or any other receipt from that question: stackoverflow.com/questions/30362391/…. Note, that YAML standard doesn't define order of the keys, so the first key extracted from subtree needn't to be the first key in the yaml file. However, there are some ways for remain that order: stackoverflow.com/questions/13297744/…– Tsyvarev
Feb 22 '18 at 15:57
but its printing the entire yaml file.
- It prints a subtree corresponded to "devices" node. If you want to get the first key of that subtree, use list(subtree.keys())[0]
or any other receipt from that question: stackoverflow.com/questions/30362391/…. Note, that YAML standard doesn't define order of the keys, so the first key extracted from subtree needn't to be the first key in the yaml file. However, there are some ways for remain that order: stackoverflow.com/questions/13297744/…– Tsyvarev
Feb 22 '18 at 15:57
add a comment |
1 Answer
1
active
oldest
votes
First you need to load the yaml file into a python nested dictionary object (key/value pairs). After this you're able to access the values of the dictionary with the dict.get('key') method.
# Read yaml file into nested dictionary
import yaml
fileName = '/tmp/testbed1.yaml'
with open(fileName, 'r') as yamlFile:
data = yaml.load(yamlFile)
# Get target value
target = data.get('devices').get('NN41_R11')
# Pretty print nested dictionary (or simply print)
__import__('pprint').pprint(target)
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
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%2f48931584%2fhow-to-print-the-first-key-by-searching-from-nested-yaml-using-python%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
First you need to load the yaml file into a python nested dictionary object (key/value pairs). After this you're able to access the values of the dictionary with the dict.get('key') method.
# Read yaml file into nested dictionary
import yaml
fileName = '/tmp/testbed1.yaml'
with open(fileName, 'r') as yamlFile:
data = yaml.load(yamlFile)
# Get target value
target = data.get('devices').get('NN41_R11')
# Pretty print nested dictionary (or simply print)
__import__('pprint').pprint(target)
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
add a comment |
First you need to load the yaml file into a python nested dictionary object (key/value pairs). After this you're able to access the values of the dictionary with the dict.get('key') method.
# Read yaml file into nested dictionary
import yaml
fileName = '/tmp/testbed1.yaml'
with open(fileName, 'r') as yamlFile:
data = yaml.load(yamlFile)
# Get target value
target = data.get('devices').get('NN41_R11')
# Pretty print nested dictionary (or simply print)
__import__('pprint').pprint(target)
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
add a comment |
First you need to load the yaml file into a python nested dictionary object (key/value pairs). After this you're able to access the values of the dictionary with the dict.get('key') method.
# Read yaml file into nested dictionary
import yaml
fileName = '/tmp/testbed1.yaml'
with open(fileName, 'r') as yamlFile:
data = yaml.load(yamlFile)
# Get target value
target = data.get('devices').get('NN41_R11')
# Pretty print nested dictionary (or simply print)
__import__('pprint').pprint(target)
First you need to load the yaml file into a python nested dictionary object (key/value pairs). After this you're able to access the values of the dictionary with the dict.get('key') method.
# Read yaml file into nested dictionary
import yaml
fileName = '/tmp/testbed1.yaml'
with open(fileName, 'r') as yamlFile:
data = yaml.load(yamlFile)
# Get target value
target = data.get('devices').get('NN41_R11')
# Pretty print nested dictionary (or simply print)
__import__('pprint').pprint(target)
edited Nov 14 '18 at 17:29
answered Nov 14 '18 at 16:07
MikeBoikoMikeBoiko
12
12
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
add a comment |
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
your answer could be better if you add some explanation. In this way you grant your answer to be useful to all levels of knowledgement
– DaFois
Nov 14 '18 at 16:30
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
Thanks for the feedback, I will do that.
– MikeBoiko
Nov 14 '18 at 17:28
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%2f48931584%2fhow-to-print-the-first-key-by-searching-from-nested-yaml-using-python%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
but its printing the entire yaml file.
- It prints a subtree corresponded to "devices" node. If you want to get the first key of that subtree, uselist(subtree.keys())[0]
or any other receipt from that question: stackoverflow.com/questions/30362391/…. Note, that YAML standard doesn't define order of the keys, so the first key extracted from subtree needn't to be the first key in the yaml file. However, there are some ways for remain that order: stackoverflow.com/questions/13297744/…– Tsyvarev
Feb 22 '18 at 15:57