[Python] How do I write out an anchor using ruamel.yaml?

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create data in python and write it to a file as a yaml-document using anchors and merges in the output.

    I think this could be possible with ruamel YAML, because, as described in ruamels official examples:

    • it can load yaml-documents with anchors and merges transparently (transparent in the sense that access to the data is the same whether it is defined through anchors or not)
    • it can round-trip yaml-files while preserving anchors (so loading a yaml-file which includes anchors can be read and rewritten to disk and still have anchors)

    This means ruamel.yaml must have an internal representation of the yaml-data that includes and understands anchors (unlike PyYAML, which only reads anchors but does not preserve them). There does not seem to be a documented way to create such anchors from python code.

    Continue reading...

Compartilhe esta Página