指定PyYAML转储(II)部分的样式:序列 [英] Specifying styles for portions of a PyYAML dump (II): sequences

查看:159
本文介绍了指定PyYAML转储(II)部分的样式:序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  import yaml 

st2 = yaml.load(
edges:
- [1,2]
- [2,1,[1,0]]

print yaml.dump(st2)
$ b $ class blockseq(dict):pass
def blockseq_rep(dumper,data):
return dumper。表达式映射(u'tag:yaml.org,2002:seq',data,flow_style = False)

类流程图(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,数据):
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 in s t2 ['edges']]
print yaml.dump(st2)

此脚本退出以下输出显示错误:

 边缘:
- [1,2]
- - 2
- 1
- [1,0]

Traceback(最近一次调用最后一次):
文件test-yaml-rep.py,第42行,在< module>
st2 ['edges'] = [blockseqtrue(x)for st2 ['edges']]
TypeError:无法将字典更新序列元素#0转换为序列
解决方案

您的问题是,我有两个类都操作在字典,而不是列表。你想要的东西,将与列表:

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

Python列表是YAML序列/ seqs。 Python字典是YAML映射/地图。


This is a follow up question to Specifying styles for portions of a PyYAML dump:

Consider following code contaning as input manually formatted YAML data. I am modifying the YAML data, but would like to keep edges on single lines in the written YAML file.

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 in st2['edges'] ]
print yaml.dump(st2)

This script exits with following output showing an error:

edges:
- [1, 2]
- - 2
  - 1
  - [1, 0]  

Traceback (most recent call last):
  File "test-yaml-rep.py", line 42, in <module>
    st2['edges'] = [ blockseqtrue(x) for x in st2['edges'] ]
TypeError: cannot convert dictionary update sequence element #0 to a sequence 

解决方案

Your problem is that the two classes I had both operate on dicts, not lists. You want something that will work with lists:

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

Python lists are YAML sequences / seqs. Python dicts are YAML mappings/maps.

这篇关于指定PyYAML转储(II)部分的样式:序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆