非特定标记-python

7qhs6swi  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(377)

大家好,我想加载一个yaml文件,解析它,并为每个非特定标记创建新的yaml文件(!元素)
我要把每一个部分都剪下来!新yaml文件示例中的元素
新建文件夹/element1/element1\u config.yml
内容应仅为:

name: element1
    gnc_script:
      - '*fcc_cores_1'
      - '*setup'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1

我的yaml看起来像这样(我原来的yml很大,有100多个!因此,对于这个问题,我使用了一个较短的版本):

elements:
  - !Element
    name: element1
    gnc_script:
      - '*fcc_cores_1'
      - '*setup'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1

  - !Element
    name: element2
    gnc_script:
      - '*fcc_cores_1'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1

  - !Element
    name: element3
    gnc_script:
      - '*fcc_cores_1'
      - '*setup'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1

你知道我怎么解决吗?
到目前为止,我所拥有的:

import yaml

with open ('elements.yml', 'r') as elements_file:
    elements_yml = yaml.load(elements_file)

elements= elements_yml["elements"]["!Element"]

for element in elements:
   os.makedirs(elements_yml["elements"]["!Element"]["name"])

   # create new yaml file with content of element
jexiocij

jexiocij1#

如果希望处理yaml文件的结构而不关心其内容,则不需要构造本机python对象。相反,请使用节点图:

import yaml, sys
from yaml.nodes import SequenceNode, MappingNode
from yaml.resolver import BaseResolver

input = """
elements:
  - !Element
    name: element1
    gnc_script:
      - '*fcc_cores_1'
      - '*setup'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1

  - !Element
    name: element2
    gnc_script:
      - '*fcc_cores_1'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1

  - !Element
    name: element3
    gnc_script:
      - '*fcc_cores_1'
      - '*setup'
    relationship:
      - projectx
      - projectxy
      - projectxyt
      # indirect related on
      - projectxyz1
"""

node = yaml.compose(input)

visited = set()

def output(node):
  yaml.serialize(node, sys.stdout)
  sys.stdout.write("...\n")

def visit(node):
  if node in visited: return
  if node.tag == "!Element":
    node.tag = BaseResolver.DEFAULT_MAPPING_TAG
    output(node)
  visited.add(node)
  if isinstance(node, SequenceNode):
    for child in node.value:
      visit(child)
  elif isinstance(node, MappingNode):
    for k,v in node.value:
      visit(k); visit(v)

visit(node)

这将产生:

name: element1
gnc_script:
- '*fcc_cores_1'
- '*setup'
relationship:
- projectx
- projectxy
- projectxyt
- projectxyz1
...
name: element2
gnc_script:
- '*fcc_cores_1'
relationship:
- projectx
- projectxy
- projectxyt
- projectxyz1
...
name: element3
gnc_script:
- '*fcc_cores_1'
- '*setup'
relationship:
- projectx
- projectxy
- projectxyt
- projectxyz1
...

该代码包括其输入和输出到stdout,用于演示;重写 output(node) 创建所需文件并用要处理的文件替换输入。
如您所见,输入中的注解不是输出的一部分。这是因为它们被pyyaml的解析器丢弃了。用Pyaml解决这个问题没有简单的方法;你可以试试ruamel,它试图保留评论,但我不知道它的api。

相关问题