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
18
votes
10 answers

Conda 'ImportError: No module named ruamel.yaml.comments'

Conda gives error when I run any command with it. Traceback (most recent call last): File "/usr/local/bin/conda", line 7, in from conda.cli.main import main File "/usr/local/lib/python2.7/dist-packages/conda/cli/__init__.py", line…
Nipun Garg
  • 507
  • 2
  • 5
  • 21
12
votes
1 answer

Why does PyYAML use generators to construct objects?

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…
Ryan
  • 123
  • 6
10
votes
2 answers

How can I add a comment to a YAML file in Python

I am writing a YAML file using https://pypi.python.org/pypi/ruamel.yaml The code is like this: import ruamel.yaml from ruamel.yaml.comments import CommentedSeq d = {} for m in ['B1', 'B2', 'B3']: d2 = {} for f in ['A1', 'A2', 'A3']: …
user3214546
  • 5,483
  • 9
  • 42
  • 82
9
votes
3 answers

Getting duplicate keys in YAML using Python

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…
jakubka
  • 626
  • 1
  • 9
  • 20
9
votes
1 answer

How to configure ruamel.yaml.dump output?

With this data structure: d = { (2,3,4): { 'a': [1,2], 'b': 'Hello World!', 'c': 'Voilà!' } } I would like to get this YAML: %YAML 1.2 --- [2,3,4]: a: - 1 - 2 b: Hello World! c: 'Voilà!' Unfortunately…
nowox
  • 19,233
  • 18
  • 91
  • 202
9
votes
1 answer

How can I get comments from a YAML file using ruamel.yaml in Python?

I'd like to get the comment strings from a YAML file I loaded using ruamel.yaml. The project documentation lacks an API reference and I can't find a relevant example. What's the right way to access the comments? import ruamel.yaml yaml = """\ %YAML…
M. Dudley
  • 26,519
  • 30
  • 137
  • 228
8
votes
2 answers

YAML Error: could not determine a constructor for the tag

This is very similar to questions/44786412 but mine appears to be triggered by YAML safe_load(). I'm using Ruamel's library and YamlReader to glue a bunch of CloudFormation pieces together into a single, merged template. Is bang-notation just not…
Matthew Patton
  • 81
  • 1
  • 1
  • 3
8
votes
1 answer

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

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:…
Satender346
  • 304
  • 3
  • 12
8
votes
2 answers

Preserving quotes in ruamel.yaml

I'm using ruamel.yaml for modifying a YAML file. My requirement is to add a value for an existing key, preserving everything else including the order. I got it to work, but looks like quotation marks of other keys/values get modified too in the…
keheliya
  • 2,103
  • 1
  • 20
  • 31
8
votes
2 answers

Preserve quotes and also add data with quotes in Ruamel

I am using Ruamel to preserve quote styles in human-edited YAML files. I have example input data as: --- a: '1' b: "2" c: 3 I read in data using: def read_file(f): with open(f, 'r') as _f: return ruamel.yaml.round_trip_load(_f.read(),…
Alex Harvey
  • 11,553
  • 2
  • 42
  • 73
7
votes
2 answers

Error when parsing yaml file : found character '%' that cannot start any token

I am trying to parse data from yaml file having some expressions similar to jinaj2 template syntax, the goal is to delete or add some items to the file. AddCodesList.yaml AddCodesList: body: list: {% for elt in customer %} - code: {{…
bcharfi
  • 91
  • 1
  • 1
  • 6
7
votes
1 answer

Prevent long lines getting wrapped in ruamel.yaml

I use the load_yaml_guess_indent(f, preserve_quotes=True) to read a YAML file, then modify it and write it back. I noticed long lines are getting wrapped when they are written back. (A line break is inserted after 80-85 characters.) Is there a…
keheliya
  • 2,103
  • 1
  • 20
  • 31
6
votes
1 answer

How to use ruamel.yaml to dump literal scalars

I've searched and found "similar" posts, but nothing that answers my question directly. I also found a stackoverflow post here, but no answers. I need to write to a yaml file using the following format: any_value: 123.4 data_points: |- 0.0, 1.0 …
Ivar Stange
  • 193
  • 9
6
votes
3 answers

best way to use ruamel.yaml to dump to string NOT to stream

In the past, I did something like some_fancy_printing_loggin_func(yaml.dump(...), ...), using the backward-compatible part of ruamel.yaml, but I want to convert my code to use the latest API so that I can take advantage of some of the new formatting…
rsaw
  • 2,907
  • 1
  • 25
  • 28
6
votes
1 answer

How get the sequences properly indented with ruamel.yaml?

With the following data from ruamel import yaml data = {1: {1:[{1:1,2:2},{1:1,2:2}], 2:2}, 2: 42} I get an incorrect indentation for the sequence >>> print yaml.round_trip_dump(data) 1: 1: - 1: 1 2: 2 - 1: 1 2: 2 2: 2 2: 42 Which…
nowox
  • 19,233
  • 18
  • 91
  • 202
1
2 3
19 20