本文介绍了在 PyYAML 中保存/转储带有注释的 YAML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的 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. Dumping (=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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:01