Questions tagged [ruamel.yaml]

ruamel.yaml is a YAML 1.2 loader/dumper library for Python, with round-trip preservation of comments, tags and flow style. It is a superset of the PyYAML package. Use this tag for issues with features or installation of that are ruamel.yaml specific.

ruamel.yaml is a YAML 1.2 loader/dumper package for Python. It was originally a derivative of Kirill Simonov’s PyYAML 3.11. Apart from supporting the YAML 1.2 from 2009, and not just the YAML 1.1 specification from 2005, it includes bugfixes and enhancements for round-tripping (i.e. load, modify, dump):

  • preservation of comments and style
  • preservation of anchor names
  • preservation of merges

ruamel.yaml also includes routines to analyse sequence indentations and control over block style sequences dumping.

Installation on most systems can be done from PyPI using:

pip install ruamel.yaml

or on modern Debian and derivatives like Ubuntu 16.04 with:

sudo apt-get install python-ruamel.yaml

The source for the package can be found on bitbucket.

286 questions
6
votes
1 answer

Modifying YAML using ruamel.yaml adds extra new lines

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,…
keheliya
  • 2,103
  • 1
  • 20
  • 31
6
votes
1 answer

Can I insert a line into ruamel.yaml's CommentedMap?

I understand that this is related to this SO question, but what I'm mostly concerned about is whether this might mess with things such as the preserved comments. import ruamel.yaml as yaml yaml_str = """\ first_name: Art occupation: Architect #…
demux
  • 4,088
  • 1
  • 25
  • 51
5
votes
2 answers

How do you remove a comment in ruamel.yaml?

I really like how ruamel.yaml can Round-trip comments, but I haven't figured out how to remove a comment from a YAML file. #!/usr/bin/env python3 from ruamel.yaml import YAML import sys yaml = YAML() yaml.preserve_quotes = True with…
earboxer
  • 106
  • 3
5
votes
1 answer

Edit existing yaml file but keeping original comments

I am trying to create a Python script that will convert our IPtables config to firewall multi within a YAML file. I originally was using pyyaml, however, later found that this removes all comments which I will need to keep, I found that ruamel.yaml…
james
  • 53
  • 4
5
votes
1 answer

Convert YAML multi-line values to folded block scalar style?

Using ruamel.yaml I tried to get a YAML in a certain style, more specifically one where single-line strings start at same line as : and multi-line strings using a folded scalar style (|/|-) and lines being limited to a certain amount of characters…
phk
  • 1,673
  • 22
  • 41
5
votes
1 answer

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

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 #…
msinn
  • 75
  • 1
  • 8
5
votes
2 answers

ruamel.yaml equivalent of sort_keys?

I'm trying to dump a Python dict to a YAML file using ruamel.yaml. I'm familiar with the json module's interface, where pretty-printing a dict is as simple as import json with open('outfile.json', 'w') as f: json.dump(mydict, f, indent=4,…
Quuxplusone
  • 19,419
  • 5
  • 72
  • 137
5
votes
1 answer

How to dump YAML with explicit references?

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…
nowox
  • 19,233
  • 18
  • 91
  • 202
4
votes
1 answer

Can I control the formatting of multiline strings?

The following code: from ruamel.yaml import YAML import sys, textwrap yaml = YAML() yaml.default_flow_style = False yaml.dump({ 'hello.py': textwrap.dedent("""\ import sys sys.stdout.write("hello world") """) },…
thebjorn
  • 22,303
  • 7
  • 72
  • 116
4
votes
2 answers

YAML - Dumping a nested object without types/tags

I'm trying to dump some Python objects out into YAML. Currently, regardless of YAML library (pyyaml, oyaml, or ruamel) I'm having an issue where calling .dump(MyObject) gives me correct YAML, but seems to add a lot of metadata about the Python…
Azarantara
  • 497
  • 2
  • 15
4
votes
1 answer

Declare data type to ruamel.yaml so that it can represen/serialize it?

I am using a function from a python library which returns an object with a specific data type. I would like to serialize that object to a yaml file and I would like to use ruamel.yaml. The problem is that ruamel.yaml does not know how to serialize…
pmav99
  • 1,748
  • 18
  • 26
4
votes
2 answers

When ruamel.yaml loads @dataclass from string, __post_init__ is not called

Assume I created a @dataclass class Foo, and added a __post_init__ to perform type checking and processing. When I attempt to yaml.load a !Foo object, __post_init__ is not called. from dataclasses import dataclass, fields from ruamel.yaml import…
nyanpasu64
  • 1,970
  • 1
  • 16
  • 27
4
votes
1 answer

Suppress !!python/unicode in YAML output

When dumping (ruamel.yaml, PyYAML) the dict data = {'abc': 'def'} as YAML (with default_flow_style=False) in Python 2.7 you will get: abc: def which is fine. However if you make all strings unicode (by u prefixing or by using from __future__…
Anthon
  • 51,019
  • 25
  • 150
  • 211
4
votes
1 answer

Insert a key using Ruamel

I am using the Ruamel Python library to programmatically edit human-edited YAML files. The source files have keys that are sorted alphabetically. I'm not sure if this is a basic Python question, or a Ruamel question, but all methods I have tried to…
Alex Harvey
  • 11,553
  • 2
  • 42
  • 73
3
votes
1 answer

How do I avoid global state when using custom constructors in ruamel.yaml?

I am using ruamel.yaml to parse a complex YAML document where certain tagged nodes require special treatment. I inject my custom parsing logic using add_multi_constructor, as recommended by the published examples. The problem is that I need to…
Pavel Kirienko
  • 868
  • 1
  • 11
  • 26
1
2
3
19 20