In newest version of ruamel-yaml 0.18.10 . If i want to preserve backslash of multiple line string. How should i do.
I use code like below.
import pathlib
from ruamel.yaml import YAML
control_file = 'xx/nist_ocp4.yml'
yaml = YAML()
yaml.preserve_quotes = True
d = yaml.load(pathlib.Path(control_file))
yaml.dump(d, pathlib.Path(control_file))
Before running code:
controls:
- id: AC-1
status: not applicable
description: "The organization:\n a. Develops, documents, and disseminates to [Assignment:\
\ or whenever a significant change occurs]"
After running code:
controls:
- id: AC-1
status: not applicable
description: "The organization:\n a. Develops, documents, and disseminates to [Assignment:
or whenever a significant change occurs]"
ruamel.yaml
doesn't preserve any internals of double quoted scalars when turning them in to Python strings, and will use the normal string dumping routines to create the scalar. Among other things, this process will remove any unnecessary escape sequences.
Since a wrapped scalar is "stitched together" with a single space when loaded, the escaped space at before the word or
is semantically equivalent to not having this there.
For literal and folded scalars ruamel.yaml
does some internal conversion to keep the line breaking points information and something similar could in principle be done for escape sequences (and line breaking points) in double quoted strings.
There is always a trade-off between trying to "normalise" elements of a YAML document on round-trip (which helps with making things consistent), and trying to fully keep things as they are (so there will be no difference). Normalising is more easy, so that is the default behaviour, and there are no plans for this particular instance to implement (optional) preservation of spurious escape sequences.
BTW the officially recommended extention for files containing YAML documents has been .yaml
since at least September 2006. YML is a derivative of XML, and at least as old as YAML.