【问题标题】:Yaml dumper outputs blank instead of null from None value. How to make it output null instead? [duplicate]Yaml 转储程序从 None 值输出空白而不是 null。如何让它输出 null 呢? [复制]
【发布时间】:2019-12-02 00:31:33
【问题描述】:

我想输出 yaml 空值,但 python 转储程序却输出了一个空格。

我正在使用 ruamel.yaml

{key: None} 

应该输出

key: null

而是输出

key:

【问题讨论】:

    标签: python yaml ruamel.yaml


    【解决方案1】:

    你应该用你自己的覆盖None 的代表:

    import sys
    import ruamel.yaml
    
    
    def my_represent_none(self, data):
        return self.represent_scalar(u'tag:yaml.org,2002:null', u'null')
    
    yaml = ruamel.yaml.YAML()
    yaml.representer.add_representer(type(None), my_represent_none)
    
    data = {'key': None}
    yaml.dump(data, sys.stdout)
    

    给出:

    key: null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2015-07-19
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      相关资源
      最近更新 更多