I have the following Python code:
import ruamel.yaml
source = open("source.yaml", "r")
target = open("target.yaml", "r")
sourceValues = ruamel.yaml.load(source, ruamel.yaml.RoundTripLoader, preserve_quotes=True)
targetValues = ruamel.yaml.load(target, ruamel.yaml.RoundTripLoader, preserve_quotes=True)
source.close()
target.close()
# some changes on target properties
targetValues['test']['something'] = sourceValues['test']['something']
with open('target.yaml', 'w') as conf:
ruamel.yaml.dump(targetValues, conf, ruamel.yaml.RoundTripDumper)
One example of YAML with comments:
test:
# some comment
something: "something" # another comment
else: 123
The above code outputs:
test:
something: "something-updated"
else: 123456
Everything works fine, fields are updated as expected, quotes are being kept, code indentation looks OK... But the comments are being lost.