ruamel.yaml

How to preserve indenting in key-value when dumping yaml

五迷三道 提交于 2020-01-15 08:12:58
问题 How to preserve indenting in key-value when dumping yaml ? I am using ruamel yaml Code: in_str='''Pets: Cat: Tom Mouse: Jerry Dog: Scooby ''' import ruamel.yaml, sys results = ruamel.yaml.load(in_str, ruamel.yaml.RoundTripLoader, preserve_quotes=True) results['Pets']['Bird']='Tweety' ruamel.yaml.dump(results, sys.stdout, ruamel.yaml.RoundTripDumper, default_flow_style=True,indent=2, block_seq_indent=2) Output : Pets: Cat: Tom Mouse: Jerry Dog: Scooby Bird: Tweety Expected Output: Pets: Cat:

Modifying YAML using ruamel.yaml adds extra new lines

我是研究僧i 提交于 2020-01-04 14:19:37
问题 I need to add an extra value to an existing key in a YAML file. Following is the code I'm using. with open(yaml_in_path, 'r') as f: doc, ind, bsi = load_yaml_guess_indent(f, preserve_quotes=True) doc['phase1'] += ['c'] with open(yaml_out_path, 'w') as f: ruamel.yaml.round_trip_dump(doc, f, indent=2, block_seq_indent=bsi) This is the input and output. Input phase1: - a # a comment. - b phase2: - d Output phase1: - a # a comment. - b - c phase2: - d How can I get rid of the new line between b

Why does PyYAML use generators to construct objects?

本小妞迷上赌 提交于 2019-12-28 20:33:11
问题 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

improper output with pyyaml

安稳与你 提交于 2019-12-25 16:41:43
问题 I have a YAML file, test.yaml : test: server_group_1: type: OS::Nova::ServerGroup properties: name: { get_param: [server_groups, 5] } policies: [ { get_param: [server_group_types, 5] } ] and when I use PyYAML to read and print the output it gives me below output, which is different from the input test: server_group_1: properties: name: get_param: - server_groups - 5 policies: - get_param: - server_group_types - 5 type: OS::Nova::ServerGroup code: import yaml print yaml.dump(yaml.load(open('

improper output with pyyaml

会有一股神秘感。 提交于 2019-12-25 16:41:37
问题 I have a YAML file, test.yaml : test: server_group_1: type: OS::Nova::ServerGroup properties: name: { get_param: [server_groups, 5] } policies: [ { get_param: [server_group_types, 5] } ] and when I use PyYAML to read and print the output it gives me below output, which is different from the input test: server_group_1: properties: name: get_param: - server_groups - 5 policies: - get_param: - server_group_types - 5 type: OS::Nova::ServerGroup code: import yaml print yaml.dump(yaml.load(open('

Preserve empty message in ruamel.yaml roundtrip parsing

社会主义新天地 提交于 2019-12-24 13:28:45
问题 Using ruamel.yaml, the output of roundtrip parsing of the YAML a: {b: } is a: {b: !!null ''} Is there any way to preserve the empty message, or overwrite the None representer to output an empty message as above? 回答1: This is not trivial and I am not sure if the solution presented below doesn't have adverse side-effects . The cause for the non-triviality has to do with multiple things: You are using the less readable flow style a: {b: } instead of block style: a: b: The latter round-trips

Parsing YAML, get line numbers even in ordered maps

◇◆丶佛笑我妖孽 提交于 2019-12-24 07:46:18
问题 I need to get the line numbers of certain keys of a YAML file. Please note, this answer does not solve the issue: I do use ruamel.yaml, and the answers do not work with ordered maps. #!/usr/bin/env python3 # -*- coding: utf-8 -*- from ruamel import yaml data = yaml.round_trip_load(""" key1: !!omap - key2: item2 - key3: item3 - key4: !!omap - key5: item5 - key6: item6 """) print(data) As a result I get this: CommentedMap([('key1', CommentedOrderedMap([('key2', 'item2'), ('key3', 'item3'), (

How to dump YAML with explicit references?

☆樱花仙子☆ 提交于 2019-12-22 06:55:29
问题 Recursive references work great in ruamel.yaml or pyyaml : $ ruamel.yaml.dump(ruamel.yaml.load('&A [ *A ]')) '&id001 - *id001' However it (obviously) does not work on normal references: $ ruamel.yaml.dump(ruamel.yaml.load("foo: &foo { a: 42 }\nbar: { <<: *foo }")) bar: {a: 42} foo: {a: 42} I would like is to explicitly create a reference: data = {} data['foo'] = {'foo': {'a': 42}} data['bar'] = { '<<': data['foo'], 'b': 43 } $ ruamel.yaml.dump(data, magic=True) foo: &foo a: 42 bar: <<: *foo b

How do I append to a YAML file with python

好久不见. 提交于 2019-12-20 07:15:43
问题 I have a YAML file called data.yaml : --- '001': name: Ben email: ben@test.com I would like to have an updated file that looks like this: --- '001': name: Ben email: ben@test.com '002': name: Lisa email: lisa@test.com numbers: - 000-111-2222 - 000-111-2223 How do I achieve this in python using yaml package/s? Edit: I have tried: import yaml import io data = {'002': {'name': 'Lisa', 'email': 'lisa@test.com', 'numbers': ['000-111-2222', '000-111-2223']}} with io.open('data.yaml', 'w', encoding=

remove single quotes from dict values while adding content to yaml file using python ruamel.yaml

ε祈祈猫儿з 提交于 2019-12-12 23:05:11
问题 I have an yaml file as mentioned below test1.yaml resources: name:{get_param: vname} ssh_keypair: {get_param: ssh_keypair} Now I want to add test1_routable_net: { get_param: abc_routable_net } under resources of test1.yaml Here is the code which I tried import ruamel.yaml yaml = ruamel.yaml.YAML() test="{ get_param: abc_routable_net }".strip(‘\’’) with open('/tmp/test1.yaml') as fp: data = yaml.load(fp) data['resources'].update({‘test1_routable_net’:test}) yaml.dump(data,file('/tes2.yaml', 'w