This is my YAML file (data.yaml
):
- Department: "IT"
name: "abcd"
Education: "Bachlore of Engineering"
And I want to edit it as follows:
- Department: "IT"
name: "abcd"
Education: "Bachlore of Engineering"
- Department: "Production"
name: "xyz"
Education: "Bachlore of Engineering"
- Department: "Management"
name: "ab"
Education: "MBA"
This is my code (currently adding only second list):
from pathlib import Path
from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import SingleQuotedScalarString, DoubleQuotedScalarString
datapath= Path('C:/Users/Master 1TB/source/repos/check2/check2/data.yaml')
with YAML(output=datapath) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(datapath)
code = [{
"Department": "Production"
"name": "xyz"
"Education": "Bachlore of Engineering"
}]
yaml.dump(code)
Now the problem when code dump the new list in the data.yaml
the earlier list gets deleted, hence my output is:
- Department: "Production"
name: "xyz"
Education: "Bachlore of Engineering"
Instead I want the previous item also in the output and as you explained in the link (How to read a component in YAML file so that I can edit it's key value using ruamel.yaml? ), I have to append the new list value, but this is possible only if I have one list value.
What can be done in this scenario?
Also I am going to add more list values in data.yaml
(preserving all the earlier list in the same YAML file).