返回介绍

9.7. YAML

发布于 2024-02-10 15:26:30 字数 3546 浏览 0 评论 0 收藏 0

9.7. YAML

9.7.1. 严格按段落展示 |、|+、|-

|:文中自动换行 + 文末新增一空行
|+:文中自动换行 + 文末新增两空行
|-:文中自动换行 + 文末不新增行		

9.7.2. >、>+、>-

>:文中不自动换行 + 文末新增一空行
>+:文中不自动换行 + 文末新增两空行
>-:文中不自动换行 + 文末不新增行		

9.7.3. PyYAML

9.7.3.1. 解决 | 问题

import sys
import yaml

x = u"""\
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6oySC+8/N9VNpk0gJS7Gk8vn9sYN7FhjpAQnoHRqTN/Oaiyx
xk2AleP2vXpojA/DHldT1JO+o3j56AHD+yfNFFeYvgWKDY35g49HsZZhbyCEAB45
...
"""

yaml.SafeDumper.org_represent_str = yaml.SafeDumper.represent_str

def repr_str(dumper, data):
    if '\n' in data:
        return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
    return dumper.org_represent_str(data)

yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper)

yaml.safe_dump(dict(a=1, b='hello world', c=x), sys.stdout)

print('-'*50)

class PSS(str):
    pass

x = PSS("""\
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6oySC+8/N9VNpk0gJS7Gk8vn9sYN7FhjpAQnoHRqTN/Oaiyx
xk2AleP2vXpojA/DHldT1JO+o3j56AHD+yfNFFeYvgWKDY35g49HsZZhbyCEAB45
...
""")

def pss_representer(dumper, data):
        style = '|'
        # if sys.versioninfo < (3,) and not isinstance(data, unicode):
        #     data = unicode(data, 'ascii')
        tag = u'tag:yaml.org,2002:str'
        return dumper.represent_scalar(tag, data, style=style)

yaml.add_representer(PSS, pss_representer, Dumper=yaml.SafeDumper)

yaml.safe_dump(dict(a=1, b='hello world', c=x), sys.stdout)

9.7.4. ruamel.yaml

pip install ruamel.yaml

9.7.4.1. 解决 | 问题

from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import PreservedScalarString as pss

x = pss("""\
external_url 'https://gitlab.example.com'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.aliyun.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "netkiller@msn.com" 
gitlab_rails['smtp_password'] = "******"
gitlab_rails['smtp_domain'] = "aliyun.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = 'netkiller@msn.com'
gitlab_rails['gitlab_shell_ssh_port'] = 22
""")

yaml = YAML()

yaml.dump(dict(a=1, b='hello world', c=x), sys.stdout)			

输出结果

a: 1
b: hello world
c: |
  external_url 'https://gitlab.example.com'
  gitlab_rails['time_zone'] = 'Asia/Shanghai'
  gitlab_rails['smtp_enable'] = true
  gitlab_rails['smtp_address'] = "smtp.aliyun.com"
  gitlab_rails['smtp_port'] = 465
  gitlab_rails['smtp_user_name'] = "netkiller@msn.com" 
  gitlab_rails['smtp_password'] = "******"
  gitlab_rails['smtp_domain'] = "aliyun.com"
  gitlab_rails['smtp_authentication'] = "login"
  gitlab_rails['smtp_enable_starttls_auto'] = true
  gitlab_rails['smtp_tls'] = true
  gitlab_rails['gitlab_email_from'] = 'netkiller@msn.com'
  gitlab_rails['gitlab_shell_ssh_port'] = 22			

9.7.4.2. LiteralScalarString, PreservedScalarString

from ruamel.yaml.scalarstring import LiteralScalarString, PreservedScalarString
from ruamel.yaml import YAML
import sys

yaml = YAML()
data = {}
data['data'] = PreservedScalarString("""\
0.0, 1.0
0.1, 1.5
0.2, 1.7
""")

data['data_points'] = LiteralScalarString("""\
0.0, 1.0
0.1, 1.5
0.2, 1.7""")

yaml.dump(data, sys.stdout)

print("=" * 50)

yaml_str = """\
any_value: 123.4
data_points: |2-
  a
  b
"""

data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)			

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文