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
  • loading/dumping of tags without class registration

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.

471 questions
28
votes
15 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
  • 628
  • 2
  • 8
  • 22
16
votes
4 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
  • 706
  • 1
  • 9
  • 23
15
votes
3 answers

Best way to use ruamel.yaml to dump YAML 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
  • 3,315
  • 2
  • 28
  • 30
14
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
  • 25,978
  • 39
  • 143
  • 293
13
votes
1 answer

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
  • 131
  • 1
  • 1
  • 4
12
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,393
  • 2
  • 24
  • 35
12
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,393
  • 2
  • 24
  • 35
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
  • 7
12
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
  • 25,978
  • 39
  • 143
  • 293
11
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
  • 352
  • 1
  • 3
  • 15
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
  • 6,523
  • 13
  • 51
  • 98
9
votes
3 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
  • 111
  • 1
  • 1
  • 6
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…
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243
8
votes
2 answers

ruamel_yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/tuple' in ""

I am trying to download the Kinetics-600 dataset for activity recognition and for that I am following the instructions in this repo: https://github.com/activitynet/ActivityNet However, at the step conda env create -f environment.yml I get this…
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
8
votes
3 answers

Use Mypy with Ruamel.yaml

I am attempting to use MyPy with modules that use ruamel.yaml and Mypy cannot find ruamel.yaml even though Python has no problem finding it. I am puzzled because I can't find a module called YAML.py or class called YAML either, even though these…
Jonathan
  • 2,635
  • 3
  • 30
  • 49
1
2 3
31 32