pythonyamltext-parsingautomated-refactoring

How to update YAML file without loss of comments and formatting / YAML automatic refactoring in Python


I would like to update YAML file values in Python without losing formatting and comments in Python. For example I would like to tranform

YAML file

value: 456                  # nice value

to

value: 6                  # nice value

with interface similar to

y = yaml.load('path')
y['value'] = 6
y.save()

Is there some way how to do it elegantly in Python (without writting a new YAML parsing library)?

I need systematic longterm maintainable solution - so no regex substitutions is okey for me since they get ugly and hardly maintainable, when you do to much of them in your code.

I haven't found any Python library which does the job. The only library I found, which is considering the feature, but is not implemented it yet, is C library libyaml(issue on Github). Have I missed any?

The problem might be also formulated as: do you know some automatic refactoring YAML library in Python?

Thanks.


Solution

  • ruamel.yaml may be what you are looking for, it is a YAML parser/emitter that supports roundtrip preservation of comments:

    import sys
    from ruamel.yaml import YAML
    
    yaml_data = "value: 456                  # nice value"
    
    yaml = YAML()
    data = yaml.load(yaml_data)
    data["value"] = 6
    
    yaml.dump(data, sys.stdout)
    

    Output:

    value: 6                    # nice value