擅长:python、mysql、java
<p><code>ruamel.yaml</code>不幸的是,<strong>没有</strong>完全保留原始格式,引用其<a href="https://yaml.readthedocs.io/en/latest/overview.html" rel="nofollow noreferrer">docs</a>:</p>
<blockquote>
<p>Although individual indentation of lines is not preserved, you can
specify separate indentation levels for mappings and sequences
(counting for sequences does not include the dash for a sequence
element) and specific offset of block sequence dashes within that
indentation.</p>
</blockquote>
<p>我不知道有哪个Python库能做到这一点。在</p>
<p>当我需要在不触及YAML文件格式的情况下更改YAML文件时,我不情愿地使用regexp(因为它几乎和<a href="https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454">parsing XHTML with it</a>一样糟糕)。在</p>
<p>请随时提出更好的解决方案,如果你知道的话,我很乐意了解它!在</p>