ruamel.yaml

How to change an anchored scalar in a sequence without destroying the anchor in ruamel.yaml?

£可爱£侵袭症+ 提交于 2019-12-02 07:30:25
问题 When using ruamel.yaml version 0.15.92 with Python 3.6.6 on CentOS 7, I cannot seem to update the value of an anchored scalar in a sequence without destroying the anchor itself or creating invalid YAML from the next dump. I have attempted to recreate the original node type with the new value (old PlainScalarString -> new PlainScalarString , old FoldedScalarString -> new FoldedScalarString , etc), copying the anchor to it. While this restores the anchor to the updated scalar value, it also

How to insert a comment line to YAML in Python using ruamel.yaml?

ε祈祈猫儿з 提交于 2019-12-01 18:18:42
问题 I have a structure like this to which I want to add comment lines using ruamel.yaml : xyz: a: 1 # comment 1 b: 2 test1: test2: test3: 3 Now, I want to insert comment-lines (not eol_comments) to make it look like this: xyz: a: 1 # comment 1 b: 2 # before test1 (top level) test1: # before test2 test2: # after test2 test3: 3 I know, that I can add eol_comments using ruamel.yaml , but I didn't find a way to add whole comment lines. 回答1: There is indeed not a function in ruamel.yaml<=0.12.18 to

how to keep null value in yaml file while dumping though ruamel.yaml

走远了吗. 提交于 2019-12-01 18:11:32
I have YAML file site.yaml : Kvm_BLOCK: ip_address: 10.X.X.X property: null server_type: zone loaded and then dumped with: ruamel.yaml.dump(site_yaml, new_file, Dumper=ruamel.yaml.RoundTripDumper) it becomes Kvm_BLOCK: ip_address: 10.X.X.X property: server_type: zone how to retain this null value in property block The null value in YAML 1.2 (constructed as Python's None ) can be represented as null , Null , NULL and ~ , as specified here . Additionally : Nodes with empty content are interpreted as if they were plain scalars with an empty value. Such nodes are commonly resolved to a “null”

How to create a custom yaml mapping dumper for ruamel.yaml?

人走茶凉 提交于 2019-12-01 07:57:47
问题 I'm trying to make a custom YAML dumper/loader for some configuration objects. For simplicity, assuming we want to dump a object of class Hero to a hero.yml file. The example which works with default dumper/loader class Hero: yaml_tag = '!Hero' def __init__(self, name, age): self.name = name self.age = age Then add the default loader/dumper by ruamel.yaml yaml.register_class(Hero) And try dump and load: h = Hero('Saber', 15) with open('config.yml', 'w') as fout: yaml.dump(h, fout) with open(

Getting duplicate keys in YAML using Python

别来无恙 提交于 2019-11-30 22:22:36
We are in need of parsing YAML files which contain duplicate keys and all of these need to be parsed. It is not enough to skip duplicates. I know this is against the YAML spec and I would like to not have to do it, but a third-party tool used by us enables this usage and we need to deal with it. File example: build: step: 'step1' build: step: 'step2' After parsing we should have a similar data structure to this: yaml.load('file.yml') # [('build', [('step', 'step1')]), ('build', [('step', 'step2')])] dict can no longer be used to represent the parsed contents. I am looking for a solution in

Getting duplicate keys in YAML using Python

三世轮回 提交于 2019-11-30 18:15:17
问题 We are in need of parsing YAML files which contain duplicate keys and all of these need to be parsed. It is not enough to skip duplicates. I know this is against the YAML spec and I would like to not have to do it, but a third-party tool used by us enables this usage and we need to deal with it. File example: build: step: 'step1' build: step: 'step2' After parsing we should have a similar data structure to this: yaml.load('file.yml') # [('build', [('step', 'step1')]), ('build', [('step',

How to read a component in YAML file so that I can edit it's key value using ruamel.yaml?

醉酒当歌 提交于 2019-11-29 08:59:21
This is my YAML file ( input.yaml ): team_member: name: Max hobbies: - Reading team_leader: name: Stuart hobbies: - dancing I want to edit this YAML file to add more values in key 'hobbies', example: team_member: name: Max hobbies: - Reading - Painting team_leader: name: Stuart hobbies: - Dancing - Fishing I tried to implement the code Anthon to fit my situation but it didn't helped at all, because the indention level of that YAML file is different from mine. Example: import sys import ruamel.yaml yaml = ruamel.yaml.YAML() # yaml.preserve_quotes = True with open('input.yaml') as fp: data =

Why does PyYAML use generators to construct objects?

爱⌒轻易说出口 提交于 2019-11-28 12:01:38
I've been reading the PyYAML source code to try to understand how to define a proper constructor function that I can add with add_constructor . I have a pretty good understanding of how that code works now, but I still don't understand why the default YAML constructors in the SafeConstructor are generators. For example, the method construct_yaml_map of SafeConstructor : def construct_yaml_map(self, node): data = {} yield data value = self.construct_mapping(node) data.update(value) I understand how the generator is used in BaseConstructor.construct_object as follows to stub out an object and

How to read a component in YAML file so that I can edit it's key value using ruamel.yaml?

笑着哭i 提交于 2019-11-28 02:22:22
问题 This is my YAML file ( input.yaml ): team_member: name: Max hobbies: - Reading team_leader: name: Stuart hobbies: - dancing I want to edit this YAML file to add more values in key 'hobbies', example: team_member: name: Max hobbies: - Reading - Painting team_leader: name: Stuart hobbies: - Dancing - Fishing I tried to implement the code Anthon to fit my situation but it didn't helped at all, because the indention level of that YAML file is different from mine. Example: import sys import ruamel