これがこのSOの質問に関連していることは理解していますが、私が最も心配しているのは、これが保存されたコメントなどを混乱させる可能性があるかどうかです。
import ruamel.yaml as yaml
yaml_str = """\
first_name: Art
occupation: Architect # This is an occupation comment
about: Art Vandelay is a fictional character that George invents...
"""
data = yaml.load(yaml_str, Loader=yaml.RoundTripLoader)
# I'd like to extend CommentedMap so that I can do something like:
data.insert(1, 'last_name', 'Vandelay')
print(yaml.dump(data, Dumper=yaml.RoundTripDumper))
出力する必要があります:
first_name: Art
last_name: Vandelay
occupation: Architect # This is an occupation comment
about: Art Vandelay is a fictional character that George invents...
出力すべきではありません:
first_name: Art
last_name: Vandelay # This is an occupation comment
occupation: Architect
about: Art Vandelay is a fictional character that George invents...