删除最后一个dict键时保留以下注释山药

2024-04-23 06:29:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用ruamel.yamlPython库从大型YAML文件中的嵌套字典中删除一些键/值对,同时保留周围的注释。下面是我使用的代码的简化版本:

import sys
import ruamel.yaml

with open(sys.argv[1], 'r') as doc:
    parsed = ruamel.yaml.round_trip_load(doc, preserve_quotes=True)

    for item in parsed['items']:
        if item['color'] == 'blue':
            del item['color']

    yaml = ruamel.yaml.YAML(typ='rt')
    yaml.indent(sequence=4, offset=2)
    yaml.dump(parsed, sys.stdout)

。。。以及一个我正试图编辑的附带文件(目的是删除“color:blue”行:

▶ cat items.yml
items:
  - name: a
    color: blue
    texture: smooth

  # This is a comment above 'c'
  # More comment
  - name: b
    texture: wrinkled
    color: yellow

对于那个特定的文件,代码实现了我想要的:

▶ ./munge.py items.yml
items:
  - name: a
    texture: smooth

  # This is a comment above 'c'
  # More comment
  - name: b
    texture: wrinkled
    color: yellow

但是,如果color: blue是第一个dict中的最后一个键/值对,则第二个项前面的注释将被吃掉:

▶ cat items.yml
items:
  - name: a
    texture: smooth
    color: blue

  # This is a comment above 'c'
  # More comment
  - name: b
    texture: wrinkled
    color: yellow
▶ ./munge.py items.yml
items:
  - name: a
    texture: smooth
  - name: b
    texture: wrinkled
    color: yellow

注释似乎附加到字典的最后一个键/值对,而不是表示为第二项前面的“前导”注释。你知道吗

即使删除dict中的最后一个键,有没有办法保留下一项之前的注释?你知道吗


Tags: 文件nameyamlymlsyscommentruamelitems
1条回答
网友
1楼 · 发布于 2024-04-23 06:29:18

注释与分析的最后一个集合节点相关联,基于 映射的键或序列的索引。你的评论 “before”实际上是在最后一行之后的行尾注释 第一个序列项的键值对,扩展到多个 线。你知道吗

如果使用print(parsed['items'][0].ca)打印dict-like对象(aCommentedMap)的comment属性,您将得到:

items={'color': [None, None, CommentToken("\n\n  # This is a comment above 'c'\n  # More comment\n", line: 5, col: 2), None]})

因此,如果从CommentedMap中删除键color,转储parsed将不再输出 注释,因为没有自动机制将注释与另一个键或某个更高级别的节点相关联。你知道吗

您可以通过跟踪循环中的上一个键来“移动”该注释:

parsed['items'][0].ca.items['texture'] = parsed['items'][0].ca.items.pop('color')

这就需要你跟踪上一个关键点:

import sys
import ruamel.yaml

yaml_str = """\
items:
  - name: a
    texture: smooth
    color: blue

  # This is a comment associated with the last key of the preceding mapping
  # More of the same comment
  - name: b
    texture: wrinkled
    color: yellow
"""

yaml = ruamel.yaml.YAML()
yaml.indent(sequence=4, offset=2)
yaml.preserve_quotes = True
parsed = yaml.load(yaml_str)
prev = None
for item in parsed['items']:
    for key in item:
        if key == 'color' and item[key] == 'blue':
            if prev is not None:
                item.ca.items[prev] = item.ca.items.pop(key)
            del item['color']
            break
        prev = key
yaml.dump(parsed, sys.stdout)

它给出:

items:
  - name: a
    texture: smooth

  # This is a comment associated with the last key of the preceding mapping
  # More of the same comment
  - name: b
    texture: wrinkled
    color: yellow

注意事项:

  • 将注释移动到父级(sequence/list/CommentedSeq)是 可能,但不是那么琐碎

  • 没有必要混合使用旧的API (ruamel.yaml.round_trip_load())和新的(yaml=YAML()

  • 应该从with语句、文件中获取for循环 可以在round_trip_load(doc)(或yaml = ruamel.yaml.YAML(); yaml.load(doc))之后关闭

  • officially recommended extension 对于YAML文件,它已经.yaml将近13年了

确保你有一个测试用例和/或别针鲁阿迈尔.亚马尔您正在使用的版本(ruamel.yaml<0.17)作为这样的注释欺骗,不能保证在将来的版本中保持相同的工作方式。你知道吗

相关问题 更多 >