我有一个带有短字符串属性的对象,以及一个很长的多行字符串属性。我想将短字符串写为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如何对待我的属性吗?我想影响顺序和风格。
答案 0 :(得分:19)
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
答案 1 :(得分:15)
爱上@lbt's approach,我得到了这段代码:
import yaml
def str_presenter(dumper, data):
if len(data.splitlines()) > 1: # check for multiline string
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
yaml.add_representer(str, str_presenter)
它使每个多行字符串成为块文字。
我试图避免猴子修补部分。 完全归功于@lbt和@ J.F.Sebastian。
答案 2 :(得分:12)
我希望其中包含\n
的任何输入都是块文字。使用yaml/representer.py
中的代码作为基础我得到了:
# -*- coding: utf-8 -*-
import yaml
def should_use_block(value):
for c in u"\u000a\u000d\u001c\u001d\u001e\u0085\u2028\u2029":
if c in value:
return True
return False
def my_represent_scalar(self, tag, value, style=None):
if style is None:
if should_use_block(value):
style='|'
else:
style = self.default_style
node = yaml.representer.ScalarNode(tag, value, style=style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
return node
a={'short': "Hello", 'multiline': """Line1
Line2
Line3
""", 'multiline-unicode': u"""Lêne1
Lêne2
Lêne3
"""}
print(yaml.dump(a))
print(yaml.dump(a, allow_unicode=True))
yaml.representer.BaseRepresenter.represent_scalar = my_represent_scalar
print(yaml.dump(a))
print(yaml.dump(a, allow_unicode=True))
<强>输出强>
{multiline: 'Line1
Line2
Line3
', multiline-unicode: "L\xEAne1\nL\xEAne2\nL\xEAne3\n", short: Hello}
{multiline: 'Line1
Line2
Line3
', multiline-unicode: 'Lêne1
Lêne2
Lêne3
', short: Hello}
After override
multiline: |
Line1
Line2
Line3
multiline-unicode: "L\xEAne1\nL\xEAne2\nL\xEAne3\n"
short: Hello
multiline: |
Line1
Line2
Line3
multiline-unicode: |
Lêne1
Lêne2
Lêne3
short: Hello
答案 3 :(得分:4)
你可以使用ruamel.yaml
及其RoundTripLoader / Dumper(免责声明:我是该软件包的作者)除了做你想要的,它支持YAML 1.2规范(从2009年开始),还有其他一些改进:
import sys
from ruamel.yaml import YAML
yaml_str = """\
short: "Hello" # does keep the quotes, but need to tell the loader
long: |
Line1
Line2
Line3
folded: >
some like
explicit folding
of scalars
for readability
"""
yaml = YAML()
yaml.preserve_quotes = True
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)
给出:
short: "Hello" # does keep the quotes, but need to tell the loader
long: |
Line1
Line2
Line3
folded: >
some like
explicit folding
of scalars
for readability
(包括评论,从之前的同一列开始)
您也可以从头开始创建此输出,然后再创建 确实需要提供额外的信息,例如折叠的明确位置。