问题描述
我有一个看起来像这样的 yaml 文件:
I have a yaml file that looks like this:
# The following key opens a door
key: value
有没有办法在保持评论的同时加载
和转储
这些数据?
Is there a way I can load
and dump
this data while maintaining the comment?
推荐答案
PyYAML 在非常低的级别(在 Scanner.scan_to_next_token
中)丢弃注释.
PyYAML throws away comments at a very low level (in Scanner.scan_to_next_token
).
虽然您可以调整或扩展它以处理整个堆栈中的注释,但这将是一个重大修改.转储
(=发出)评论似乎更容易,并且在 ticket 114 在旧的 PyYAML 错误跟踪器上.
While you could adapt or extend it to handle comments in its whole stack, this would be a major modification. Dump
ing (=emitting) comments seems to be easier and was discussed in ticket 114 on the old PyYAML bug tracker.
截至 2020 年,关于添加对加载评论的支持的功能请求仍处于停滞状态.
As of 2020, the feature request about adding support for loading comments is still stalling.
这篇关于在 PyYAML 中保存/转储带有注释的 YAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!