接上文:Python之ruamel.yaml模块详解(一); 以下为官网的几个案例: 4 将YAML解析为Python对象并修改 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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)
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行# example
name:
# details
family: Smith # very common
given: Bob # one of the siblings
5 使用旧API将YAML解析为Python对象并修改 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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='')
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行# example
name:
# details
family: Smith # very common
given: Bob # one of the siblings
6 使用[]和.get()访问合并的键: 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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()
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行[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
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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()
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行[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()方法插入内容 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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()
来更改此默认缩进: 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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')
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行a:
b: 2
c:
- 3
- 4
0123456789
a:
b: 2
c:
- 3
- 4
0123456789
9 使用yaml.compact()隔行显示 如果一个块序列或块映射是一个序列的元素,则默认情况下会显示紧凑的符号 这意味着“父”序列的破折号与第一个元素resp位于同一行; 子集合的第一个键/值对。 如果希望其中一个或两个(序列内的序列,序列内的映射)从下一行开始,可以使用yaml.compact()
: 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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)
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行- b: 2
- - 3
- 4
===============
-
b: 2
-
- 3
- 4
10 同一数据使用三个转储,生成一个包含三个文档的流 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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)
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行---
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输出作为字符串 代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行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)))
代码语言: python
代码 运行次数:0
复制 Cloud Studio 代码运行yaml.dump((dict(a=1, b=2)), sys.stdout)
print() # or sys.stdout.write('n')