Aim is to use Python to split the following file,
parent_one:
vowel: 1075328579
audience: false
manufacturing: 951153484.6646092
thirty:
so:
law: false
rush: -1352468562.2461486
porch: -1779099534
as: true
honor: false
he: likely
tropical: false
instrument: true
home: 31507109
moving: ask
question: true
nice: make
nation: were
parent_two:
simply: chicken
apartment:
refused:
electric: compare
to: false
tropical: doubt
biggest: 674144373.7861345
fort: canal
highway: -89001716.80570006
bean: true
pupil: -564383673.7520447
been: false
affect: -1693100216
course: false
cow: true
breakfast: -359306489.2780776
got: true
into two separate files with the following content.
parent_one:
vowel: 1075328579
audience: false
manufacturing: 951153484.6646092
thirty:
so:
law: false
rush: -1352468562.2461486
porch: -1779099534
as: true
honor: false
he: likely
tropical: false
instrument: true
home: 31507109
moving: ask
question: true
nice: make
nation: were
parent_two:
simply: chicken
apartment:
refused:
electric: compare
to: false
tropical: doubt
biggest: 674144373.7861345
fort: canal
highway: -89001716.80570006
bean: true
pupil: -564383673.7520447
been: false
affect: -1693100216
course: false
cow: true
breakfast: -359306489.2780776
got: true
Python Code
Install PyYAML.
pipenv install PyYAML
Sample source:
import yaml
with open('singlefile.yml', 'r') as inputfile:
docs = yaml.safe_load_all(inputfile)
for d in docs:
for parent in d.keys():
output_file_name = parent+".yml"
out_file=open(output_file_name,"w")
d[parent] = {parent: d[parent]}
yaml.dump(d[parent], out_file)
out_file.close()
After running the source, the following files should be created.
