ruamel.yaml

Preserving long single line string as is when round-triping in ruamel

Preserving long single line string as is when round-triping in ruamel Question: In the below code, I’m trying to write a load and write a YAML string back to ensure that it retains the spacing as is. import ruamel.yaml yaml_str = """ long_single_line_text: Hello, there. This is nothing but a long single line text which …

Total answers: 1

ruamel.yaml adds incorrect indentation indicator

ruamel.yaml adds incorrect indentation indicator Question: We use Python3 (3.10) and ruamel.yaml (0.17.21) to run some validation on Kubernetes YAML manifests generated from Helm. One of them outputs a config that starts with a newline followed by an empty object {}. import sys import ruamel.yaml yaml = ruamel.yaml.YAML() yaml.indent(sequence=4, offset=2) input = """ data: abc.yaml: …

Total answers: 1

How can I maintain formatting while updating a yaml file using ruamel?

How can I maintain formatting while updating a yaml file using ruamel? Question: I have a multi-document YAML file. I am interested in modifying the third document only (this modification will be later made using other code and conditions). After some research, I selected ruamel since it was reported to preserve order and format. My …

Total answers: 1

How to edit a file with multiple YAML documents in Python

How to edit a file with multiple YAML documents in Python Question: I have the following YAML file: apiVersion: apps/v1 kind: Deployment metadata: name: nodejs namespace: test labels: app: hello-world spec: selector: matchLabels: app: hello-world replicas: 100 template: metadata: labels: app: hello-world spec: containers: – name: hello-world image: test/first:latest ports: – containerPort: 80 resources: limits: …

Total answers: 2

How can I dump YAML 1.1 booleans (y, n, T, F, etc) as quoted strings with ruamel.yaml

How can I dump YAML 1.1 booleans (y, n, T, F, etc) as quoted strings with ruamel.yaml Question: Already read https://stackoverflow.com/a/61252180/1676006 and it doesn’t seem to have solved my problem. Using ruamel.yaml: yaml = YAML(typ="safe") yaml.version = (1, 1) yaml.default_flow_style = None yaml.dump(stuff, sys.stdout) With a python dict stuff containing: { "key": "Y" } outputs …

Total answers: 1

Avoiding quotes in yaml field value

Avoiding quotes in yaml field value Question: I have a yaml file with content ip: modules: shutdown-manager: version: 0.5 package: repo: github path: ~/path manager: no This is my program import ruamel.yaml yaml = ruamel.yaml.YAML() with open(‘manifest.yml’) as stream: documents = yaml.load(stream) en=(documents[‘ip’][‘modules’][‘shutdown-manager’]) en.insert(1, ‘enabled’, ‘false’) print(en) with open(‘manifest_new.yml’, ‘wb’) as stream: yaml.dump(documents, stream) This …

Total answers: 2

Parse YAML with dots delimiter in keys

Parse YAML with dots delimiter in keys Question: We use YAML configuration for services scaling. Usually it goes like this: service: scalingPolicy: capacity: min: 1 max: 1 So it’s easy to open with basic PyYAML and parse as an dict to get config[‘service’][‘scalingPolicy’][‘capacity’][‘min’] result as 1. Problem is that some configs are built with dots …

Total answers: 2

How do I read/write markdown yaml frontmatter with ruamel.yaml?

How do I read/write markdown yaml frontmatter with ruamel.yaml? Question: I want to use Python to read and write YAML frontmatter in markdown files. I have come across the ruamel.yaml package but am having trouble understanding how to use it for this purpose. If I have a markdown file: — car: make: Toyota model: Camry …

Total answers: 1

ruamel.yaml: pin comment to next data item instead of previous one

ruamel.yaml: pin comment to next data item instead of previous one Question: hI observed a somewhat confusing behavior when using ruamel.yaml with roundtrip loader. It’s probably because it’s not trivial for ruamel.yaml to automatically determine to which data item a comment should be connected. In the below example, I would like to always keep the …

Total answers: 1

Is 'yes' really an alias for 'true' according to the YAML 1.1 spec? The 1.2 spec?

Is 'yes' really an alias for 'true' according to the YAML 1.1 spec? The 1.2 spec? Question: I’m trying to debug an issue, and it boils down to… >>> import yaml as pyyaml >>> import ruamel.yaml as ruamel >>> ruamel.load(“x: yes”) == ruamel.load(“x: true”) False >>> pyyaml.safe_load(“x: yes”) == pyyaml.safe_load(“x: true”) True There are rumors …

Total answers: 3