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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
PyYAML throws away comments at a very low level (in
Scanner.scan_to_next_token).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.As of 2023, the feature request about adding support for loading comments is still stalling.