如何控制PyYAML用于数据的标量形式?

问题描述

基于Python中是否有支持将长字符串转储为块文字或折叠块的yaml库?

import yaml
from collections import OrderedDict

class quoted(str):
    pass

def quoted_presenter(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')
yaml.add_representer(quoted, quoted_presenter)

class literal(str):
    pass

def literal_presenter(dumper, data):
    return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
yaml.add_representer(literal, literal_presenter)

def ordered_dict_presenter(dumper, data):
    return dumper.represent_dict(data.items())
yaml.add_representer(OrderedDict, ordered_dict_presenter)

d = OrderedDict(short=quoted("Hello"), long=literal("Line1\nLine2\nLine3\n"))

print(yaml.dump(d))

输出

short: "Hello"
long: |
  Line1
  Line2
  Line3

解决方法

我有一个带有短字符串属性和长多行字符串属性的对象。我想将短字符串写为YAML引用标量,将多行字符串写为文字标量:

my_obj.short = "Hello"
my_obj.long = "Line1\nLine2\nLine3"

我希望YAML看起来像这样:

short: "Hello"
long: |
  Line1
  Line2
  Line3

我该如何指示PyYAML执行此操作?如果我调用yaml.dump(my_obj),它将产生类似dict的输出:

{long: 'line1

    line2

    line3

    ',short: Hello}

(不知道为什么长距离是这样的两倍…)

我可以要求PyYAML如何处理我的属性吗?我想同时影响顺序和样式。