在新创建的ruamel.yaml.comments.CommentedMap
上使用.yaml_set_comment_before_after_key()
和.yaml_add_eol_comment()
所做的注释不会显示在转储字符串中.请参阅下面的预期输出与实际输出.
请注意,ruamel.yaml==0.17.4是产生预期输出的最新版本.任何晚于此版本的版本都不会(见下文).
# Minimal reproducible example
import ruamel.yaml
data = ruamel.yaml.comments.CommentedMap()
data['key'] = 'value'
data.yaml_set_start_comment('Start comment', indent=0)
data.yaml_set_comment_before_after_key('key', before='Before comment', indent=0)
data.yaml_add_eol_comment('End-of-line comment', 'key', column=0)
yaml_str = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
print(yaml_str)
Using:
Python 3.10.8
ruamel.yaml==0.17.32
ruamel.yaml.clib==0.2.7
By going through ruamel.yaml's version history, I have found that the latest ruamel version that produces the expected output to be 0.17.4 (Can be installed using pip install ruamel.yaml==0.17.4
)
I don't understand why versions higher than this do not produce the expected output anymore. Is this perhaps a bug since April 2021?
预期输出:
# Start comment
# Before comment
key: value # End-of-line comment
实际yields (使用ruamel.yaml==0.17.32):
# Start comment
key: value