【问题标题】:Dump tuple into YAML file in a specific format以特定格式将元组转储到 YAML 文件中
【发布时间】:2019-12-22 07:09:32
【问题描述】:

我想将元组转储到 YAML 文件中的特定格式。我尝试运行以下代码以将元组转储到 YAML 文件中。在下面的代码中,我尝试使用 append() 将一对元组 (x, y) 添加到列表中。之后,我会将列表转储回 YAML 文件中。我的问题是,当我转储回 YAML 文件时,是否可以保持文件的相同特定格式?

import ruamel.yaml

def addObstacles():

    yaml = ruamel.yaml.YAML()
    with open('input.yaml') as f:
        doc = yaml.load(f)

    x = 5
    y = 6
    doc['map']['obstacles'].append(list((x,y)))

    with open('input.yaml', 'w') as f:
        yaml.dump(doc, f)

以下文件的输出如下:

map:
  dimensions: [8, 8]
  obstacles:
  - !!python/tuple [4, 5]
  - !!python/tuple [2, 0]
  - !!python/tuple [1, 1]
  - !!python/tuple [0, 5]
  - !!python/tuple [2, 5]
  - !!python/tuple [4, 4]
  - !!python/tuple [7, 5]
  - !!python/tuple [1, 4]
  - !!python/tuple [6, 7]
  - !!python/tuple [6, 3]
  - !!python/tuple [1, 7]
  - !!python/tuple [3, 6]
  - - 5
    - 6

文件的预期输出如下:

map:
  dimensions: [8, 8]
  obstacles:
  - !!python/tuple [4, 5]
  - !!python/tuple [2, 0]
  - !!python/tuple [1, 1]
  - !!python/tuple [0, 5]
  - !!python/tuple [2, 5]
  - !!python/tuple [4, 4]
  - !!python/tuple [7, 5]
  - !!python/tuple [1, 4]
  - !!python/tuple [6, 7]
  - !!python/tuple [6, 3]
  - !!python/tuple [1, 7]
  - !!python/tuple [3, 6]
  - !!python/tuple [5, 6]

【问题讨论】:

    标签: python yaml ruamel.yaml


    【解决方案1】:

    由于你附加了一个列表,你会得到一个列表,但我假设你尝试过,因为一个普通的元组会 不起作用,因为您实际上在 ruamel.yaml 中发现了一个错误。

    这可以通过两行代码轻松解决,并指定default_flow_style

    import sys
    import ruamel.yaml
    
    if ruamel.yaml.version_info < (0, 16, 7):
       ruamel.yaml.representer.RoundTripRepresenter.add_representer(tuple, 
                             ruamel.yaml.representer.Representer.represent_tuple)
    
    def addObstacles():
    
        yaml = ruamel.yaml.YAML()
        yaml.default_flow_style = None   # default is False, which would get you a block sequence
        with open('input.yaml') as f:
            doc = yaml.load(f)
        doc['map']['obstacles'].append((5, 6))
    
        yaml.dump(doc, sys.stdout)
    
    
    addObstacles()
    

    这样你就得到你想要的:

    map:
      dimensions: [8, 8]
      obstacles:
      - !!python/tuple [4, 5]
      - !!python/tuple [2, 0]
      - !!python/tuple [1, 1]
      - !!python/tuple [0, 5]
      - !!python/tuple [2, 5]
      - !!python/tuple [4, 4]
      - !!python/tuple [7, 5]
      - !!python/tuple [1, 4]
      - !!python/tuple [6, 7]
      - !!python/tuple [6, 3]
      - !!python/tuple [1, 7]
      - !!python/tuple [3, 6]   # add one after this
      - !!python/tuple [5, 6]
    

    (我在使用的 YAML 输入文件中添加了注释)。

    【讨论】:

    • 我没有意识到我发现了一个错误,再次感谢@Anthon!
    【解决方案2】:

    我不确定你是否真的需要ruamel.yamlPyYAML 可以满足您开箱即用的需求:

    import yaml
    
    def addObstacles():
        with open('in.yaml') as f:
            doc = yaml.load(f)
    
        doc['map']['obstacles'].append((5, 6))
    
        with open('out.yaml', 'w') as f:
            yaml.dump(doc, f)
    

    【讨论】:

    • 虽然这可能适用于 OP 的受限示例,但如果您建议降级到 PyYAML,您至少应该列出这样做的缺点。
    猜你喜欢
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 2017-09-13
    • 2014-06-11
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多