相关疑难解决方法(0)

指定 PyYAML 转储部分的样式 (II):序列

这是指定PyYAML 转储部分的样式的后续问题:

考虑以下包含手动格式化 YAML 数据作为输入的代码。我正在修改 YAML 数据,但希望在写入的 YAML 文件中将边缘保留在单行上。

import yaml

st2 = yaml.load("""
edges:
- [1, 2]
- [2, 1, [1,0]]
""")
print yaml.dump(st2)

class blockseq( dict ): pass
def blockseq_rep(dumper, data):
    return dumper.represent_mapping( u'tag:yaml.org,2002:seq', data, flow_style=False )

class flowmap( dict ): pass
def flowmap_rep(dumper, data):
    return dumper.represent_mapping( u'tag:yaml.org,2002:map', data, flow_style=True )

class blockseqtrue( dict ): pass
def blockseqtrue_rep(dumper, data):
    return dumper.represent_mapping( u'tag:yaml.org,2002:seq', data, flow_style=True )

yaml.add_representer(blockseq, blockseq_rep)
yaml.add_representer(blockseqtrue, blockseqtrue_rep)
yaml.add_representer(flowmap, flowmap_rep)

st2['edges'] = [ blockseqtrue(x) for x …
Run Code Online (Sandbox Code Playgroud)

python formatting yaml

7
推荐指数
1
解决办法
1865
查看次数

标签 统计

formatting ×1

python ×1

yaml ×1