简体   繁体   中英

Retrieve comments at the head of the document using ruamel.yaml

Is there a method to retrieve the comments at the head of a YAML document using ruamel.yaml once it is loaded?

For example:

yaml_str = """\
# comment at head of document
date: 20210326 # comment about key-pair
"""

I know how to retrieve the comment for date :

from ruamel.yaml import YAML
yml = YAML(typ='rt')
data = yml.load(yaml_str)
comments = data.ca.items.get('date')

or for any other field in the document , but not the initial comments.

Currently there no methods to get this data.

The reason why you can't find the comment at the beginning, is because it is handled differently compared to all the other comments. When the other comments are seen in the input stream, there are data nodes parsed on which the comment can be attached, but that is of course not the case for the comment at the beginning before any data.

You can retrieve these comments, but you should make sure you check for ruamel.yaml's version, because changes to how the comments are attached have been announced

import ruamel.yaml

yaml_str = """\
# comment at head of document

# another one after empty line
date: 20210326 # comment about key-pair
"""


yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)

if ruamel.yaml.version_info <= (0, 17, 0):
     comments = [x.value for x in data.ca.comment[1]]
else:
     raise NotImplementedError

print(comments)

which gives:

['# comment at head of document\n\n', '# another one after empty line\n']

The .comment attribute of .ca will almost certainly disappaear in future releases, so you can just use try-except. And the collation of the empty line to the preceding comment as an extra newline, will also change in format, but will most likely get stable access methods at the time this happens (so you should not have to upgrade your code more than once).

That's the way I've found with inspecting objects:

>>> data.ca.comment[1][0].value
'# comment at head of document\n'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM