Python之ruamel.yaml模块详解

Posted 虫无涯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之ruamel.yaml模块详解相关的知识,希望对你有一定的参考价值。

Python之ruamel.yaml模块详解(二)

4 将YAML解析为Python对象并修改

import sys
from ruamel.yaml import YAML

inp = """\\
# example
name:
  # details
  family: Smith   # very common
  given: Alice    # one of the siblings
"""

yaml = YAML()
code = yaml.load(inp)
code['name']['given'] = 'Bob'

yaml.dump(code, sys.stdout)
  • 结果是:
# example
name:
  # details
  family: Smith   # very common
  given: Bob      # one of the siblings

5 使用旧API将YAML解析为Python对象并修改

from __future__ import print_function

import sys
import ruamel.yaml

inp = """\\
# example
name:
  # details
  family: Smith   # very common
  given: Alice    # one of the siblings
"""

code = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
code['name']['given'] = 'Bob'

ruamel.yaml.dump(code, sys.stdout, Dumper=ruamel.yaml.RoundTripDumper)

# the last statement can be done less efficient in time and memory with
# leaving out the end='' would cause a double newline at the end
# print(ruamel.yaml.dump(code, Dumper=ruamel.yaml.RoundTripDumper), end='')
  • 结果是:
# example
name:
  # details
  family: Smith   # very common
  given: Bob      # one of the siblings

6 使用[]和.get()访问合并的键:

from ruamel.yaml import YAML

inp = """\\
- &CENTER x: 1, y: 2
- &LEFT x: 0, y: 2
- &BIG r: 10
- &SMALL r: 1
# All the following maps are equal:
# Explicit keys
- x: 1
  y: 2
  r: 10
  label: center/big
# Merge one map
- <<: *CENTER
  r: 10
  label: center/big
# Merge multiple maps
- <<: [*CENTER, *BIG]
  label: center/big
# Override
- <<: [*BIG, *LEFT, *SMALL]
  x: 1
  label: center/big
"""


def func():
    yaml = YAML()
    data = yaml.load(inp)
    print(data)
    print(data[7]['y'])
    assert data[7]['y'] == 2
 

if __name__ == "__main__":
    func()
  • 结果是:
[ordereddict([('x', 1), ('y', 2)]), 
ordereddict([('x', 0), ('y', 2)]), 
ordereddict([('r', 10)]), 
ordereddict([('r', 1)]), 
ordereddict([('x', 1), ('y', 2), ('r', 10), ('label', 'center/big')]), ordereddict([('r', 10), ('label', 'center/big'), ('x', 1), ('y', 2)]), ordereddict([('label', 'center/big'), ('x', 1), ('y', 2), ('r', 10)]), ordereddict([('x', 1), ('label', 'center/big'), ('r', 10), ('y', 2)])]
2
  • 使用.get()方法,如下:
from ruamel.yaml import YAML

inp = """\\
- &CENTER x: 1, y: 2
- &LEFT x: 0, y: 2
- &BIG r: 10
- &SMALL r: 1
# All the following maps are equal:
# Explicit keys
- x: 1
  y: 2
  r: 10
  label: center/big
# Merge one map
- <<: *CENTER
  r: 10
  label: center/big
# Merge multiple maps
- <<: [*CENTER, *BIG]
  label: center/big
# Override
- <<: [*BIG, *LEFT, *SMALL]
  x: 1
  label: center/big
"""


def func():
    yaml = YAML()
    data = yaml.load(inp)
    print(data)
    print(f"1、[]方法为:data[7]['y']")
    print(f"2、.get()方法为:data[7].get('y')")
    assert data[7]['y'] == 2


if __name__ == "__main__":
    func()
  • 结果是:
[ordereddict([('x', 1), ('y', 2)]), 
ordereddict([('x', 0), ('y', 2)]), 
ordereddict([('r', 10)]), 
ordereddict([('r', 1)]), 
ordereddict([('x', 1), ('y', 2), ('r', 10), ('label', 'center/big')]), ordereddict([('r', 10), ('label', 'center/big'), ('x', 1), ('y', 2)]), ordereddict([('label', 'center/big'), ('x', 1), ('y', 2), ('r', 10)]), ordereddict([('x', 1), ('label', 'center/big'), ('r', 10), ('y', 2)])]
[]方法为:2
.get()方法为:2

7 使用insert()方法插入内容

import sys
from ruamel.yaml import YAML

yaml_str = """\\
first_name: Art
occupation: Architect  # This is an occupation comment
about: Art Vandelay is a fictional character that George invents...
"""

yaml = YAML()
data = yaml.load(yaml_str)
data.insert(1, 'last name', 'Vandelay', comment="new key")
yaml.dump(data, sys.stdout)
  • 结果为以下,可以发现插入了数据last name: Vandelay
first_name: Art
last name: Vandelay    # new key
occupation: Architect  # This is an occupation comment
about: Art Vandelay is a fictional character that George invents...

8 使用yaml.indent()更改默认缩进

  • 默认情况下,ruamel.yaml以块样式缩进两个位置,用于映射和序列;
  • 对于序列,缩进计算到标量的开头,破折号位于缩进的“空格”的第一个位置;
  • 可以通过例如使用yaml.indent()来更改此默认缩进:
import sys
from ruamel.yaml import YAML

d = dict(a=dict(b=2),c=[3, 4])
yaml = YAML()
yaml.dump(d, sys.stdout)
print('0123456789')
yaml = YAML()
yaml.indent(mapping=4, sequence=6, offset=3)
yaml.dump(d, sys.stdout)
print('0123456789')
  • 结果为:
a:
  b: 2
c:
 - 3
 - 4
0123456789
a:
    b: 2
c:
 -  3
 -  4
0123456789

9 使用yaml.compact()隔行显示

  • 如果一个块序列或块映射是一个序列的元素,则默认情况下会显示紧凑的符号
  • 这意味着“父”序列的破折号与第一个元素resp位于同一行;
  • 子集合的第一个键/值对。
  • 如果希望其中一个或两个(序列内的序列,序列内的映射)从下一行开始,可以使用yaml.compact()
import sys
from ruamel.yaml import YAML

d = [dict(b=2), [3, 4]]
yaml = YAML()
yaml.dump(d, sys.stdout)
print('='*15)
yaml = YAML()
yaml.compact(seq_seq=False, seq_map=False)
yaml.dump(d, sys.stdout)
  • 结果是:
- b: 2
- - 3
  - 4
===============
-
  b: 2
-
  - 3
  - 4

10 同一数据使用三个转储,生成一个包含三个文档的流

import sys
from ruamel.yaml import YAML

data = 1: 1: [1: 1, 2: 2, 1: 1, 2: 2], 2: 2, 2: 42

yaml = YAML()
yaml.explicit_start = True
yaml.dump(data, sys.stdout)
yaml.indent(sequence=4, offset=2)
yaml.dump(data, sys.stdout)


def sequence_indent_four(s):
    # this will fail on direclty nested lists: 1; [[2, 3], 4]
    levels = []
    ret_val = ''
    for line in s.splitlines(True):
        ls = line.lstrip()
        indent = len(line) - len(ls)
        if ls.startswith('- '):
            if not levels or indent > levels[-1]:
                levels.append(indent)
            elif levels:
                if indent < levels[-1]:
                    levels = levels[:-1]
            # same -> do nothing
        else:
            if levels:
                if indent <= levels[-1]:
                    while levels and indent <= levels[-1]:
                        levels = levels[:-1]
        ret_val += '  ' * len(levels) + line
    return ret_val

yaml = YAML()
yaml.explicit_start = True
yaml.dump(data, sys.stdout, transform=sequence_indent_four)
  • 结果是:
---
1:
  1:
 - 1: 1
    2: 2
 - 1: 1
    2: 2
  2: 2
2: 42
---
1:
  1:
    - 1: 1
      2: 2
    - 1: 1
      2: 2
  2: 2
2: 42
---
1:
  1:
    - 1: 1
      2: 2
    - 1: 1
      2: 2
  2: 2
2: 42

11 将YAML输出作为字符串

  • 以下是新API的实现方法:
import sys
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO

class MyYAML(YAML):
    def dump(self, data, stream=None, **kw):
        inefficient = False
        if stream is None:
            inefficient = True
            stream = StringIO()
        YAML.dump(self, data, stream, **kw)
        if inefficient:
            return stream.getvalue()

yaml = MyYAML()   # or typ='safe'/'unsafe' etc
print(yaml.dump(dict(a=1, b=2)))
  • 旧API是如下:
yaml.dump((dict(a=1, b=2)), sys.stdout)
print()  # or sys.stdout.write('\\n')

以上是关于Python之ruamel.yaml模块详解的主要内容,如果未能解决你的问题,请参考以下文章

Python之ruamel.yaml模块详解

Conda 'ImportError:没有名为 ruamel.yaml.comments 的模块'

如何在 Python 中使用 ruamel.yaml 从 YAML 文件中获取注释?

python ruamel.yaml 包,如何获取标题注释行?

python接口自动化测试二十九:yaml配置文件的写和读

ruamel.yaml的使用