Created
March 1, 2020 12:56
-
-
Save LouiS0616/164fc8db572411080e8128d4e0be4861 to your computer and use it in GitHub Desktop.
pyyamlについて:タグの加え方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yaml | |
from yaml import ScalarNode, SequenceNode | |
def _flatten(_, node): | |
def _inner(n): | |
if isinstance(n, ScalarNode): | |
yield n | |
elif isinstance(n, SequenceNode): | |
for e in n.value: | |
yield from _inner(e) | |
else: | |
assert False | |
return [v.value for v in _inner(node)] | |
yaml.add_constructor('!Flatten', _flatten) | |
# | |
with open('sample.yml') as fin: | |
data = yaml.load(fin.read(), Loader=yaml.FullLoader) | |
print(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a: &nums [4, 5, 6] | |
b: !!Flatten [1, 2, 3, *nums] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment