3
ruamel.yaml.RoundTripRepresenter.add_representer(type(None), represent)

0.14.X在ruamel.yaml 包的版本中工作正常

如何在 ruamel.yaml 包的旧版本(<= 0.11)中使用相同的功能或在以前版本的包中使用的替代方式?

这是我的程序示例

def represent(self, data):
    return self.represent_scalar(u'tag:yaml.org,2002:null', u'NULL')

ruamel.yaml.RoundTripRepresenter.add_representer(type(None), represent)

data = ruamel.yaml.round_trip_load(input.yaml)
ruamel.yaml.round_trip_dump(data, output.yaml)

在运行上面的代码时遇到错误

ruamel.yaml.RoundTripRepresenter.add_representer(type(None), represent_none)\nAttributeError: 'module' object has no attribute 'RoundTripRepresenter'

4

1 回答 1

0

在旧版本ruamel.yamlRoundTripRepresenter(defined in representer.py) 中没有导入main.py,因此无法通过__init__.py(which does
from ruamel.yaml.main import *)

所以你应该从源头导入它:

from ruamel.yaml.representer import RoundTripRepresenter

RoundTripRepresenter.add_representer(type(None), represent)
于 2017-06-22T17:45:24.853 回答