How to combine YAMLfiles in Python?

I have some Kubernetes YAML files which need to combined.

For that I tried using Python.

Second file (sample.yaml) should be merged to the first file source.yaml Like, the source.yaml has one section sample:, where the complete sample.yaml should be dumped.

So, I tried using below code.

#pip install pyyaml
import yaml

def yaml_loader(filepath):
    #Loads a yaml file
    with open(filepath,'r')as file_descriptor:
        data = yaml.load(file_descriptor)
    return data

def yaml_dump(filepath,data):
    with open(filepath,"w") as file_descriptor:
        yaml.dump(data, file_descriptor)


if __name__ == "__main__":
    file_path1 = "source"
    data1 = yaml_loader(file_path1)
    file_path2 = "sample.yaml"

    with open(file_path2, 'r') as file2:
        sample_yaml = file2.read()
    data1['data']['sample'] = sample_yml
    yaml_dump("temp.yml", data1)

This is creating a new file temp.yml but instead of line breaks, it is saving \n as strings.

final file snap

How can I fix this?


Stackoverflow.com is the correct place for a question like this. The reason they are closing your question is because you're asking it wrong.

As much as you're wanting to merge two file, in python you're loading two yaml files into dictionaries, merging those two dictionaries and then dumping the resultant dictionary to a single file. Note below I've added some yaml dumper options that should help the latter usage.

Your data = yaml.load(file_descriptor) in the yaml_loader() function may need a change to data = yaml.load(file_descriptor, Loader=yaml.SafeLoader)

Your new main function would look something like

if __name__ == "__main__":
    file_path1 = "source.yaml"
    file_path2 = "sample.yaml"

    # read both yaml files as Dictionaries
    data1 = yaml_loader(file_path1)
    data2 = yaml_loader(file_path2)

    # Merge the dictionaries
    data1.update(data2) # Feel free to reverse the order of data1 and data2 and then update the dumper below
   
    # Write the merged dictionary to a new file
    with open("temp.yml", 'w') as yaml_output:
      yaml_dump(data1, yaml_output, default_flow_style=False, explicit_start=True, allow_unicode=True )

Edit: For Stackoverflow the question would be "In Python: How do I load two yaml files as dictionaries, merge them and then write the result to a third file?" (you could probably google that one) instead of "How do I merge two files"