YAML+PyYAML笔记 7 | PyYAML源码之yaml.compose_all(),yaml.load(),yaml.l
【摘要】 1 yaml.compose_all()源码:作用:分析流中的所有YAML文档,并产生相应的表示树。解析:# -*- coding:utf-8 -*-# 作者:虫无涯# 日期:2023/7/28 # 文件名称:pyyaml_compose_all.py# 作用:yaml.compose_all()# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/...
1 yaml.compose_all()
- 源码:
- 作用:分析流中的所有
YAML
文档,并产生相应的表示树。 - 解析:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28
# 文件名称:pyyaml_compose_all.py
# 作用:yaml.compose_all()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import yaml
doc = """
---
user1:
name: xiaoming
age: 23
password: 123456
---
user2:
name: xiaozhang
age: 24
password: 123456
---
user3:
name: xiaoli
age: 25
password: 123456
"""
trees = yaml.compose_all(doc)
for tree in trees:
print(tree)
- 输出:
MappingNode(tag='tag:yaml.org,2002:map', value=[(ScalarNode(tag='tag:yaml.org,2002:str', value='user1'), MappingNode(tag='tag:yaml.org,2002:map', value=[(ScalarNode(tag='tag:yaml.org,2002:str', value='name'), ScalarNode(tag='tag:yaml.org,2002:str', value='xiaoming')), (ScalarNode(tag='tag:yaml.org,2002:str', value='age'), ScalarNode(tag='tag:yaml.org,2002:int', value='23')), (ScalarNode(tag='tag:yaml.org,2002:str', value='password'), ScalarNode(tag='tag:yaml.org,2002:int', value='123456'))]))])
MappingNode(tag='tag:yaml.org,2002:map', value=[(ScalarNode(tag='tag:yaml.org,2002:str', value='user2'), MappingNode(tag='tag:yaml.org,2002:map', value=[(ScalarNode(tag='tag:yaml.org,2002:str', value='name'), ScalarNode(tag='tag:yaml.org,2002:str', value='xiaozhang')), (ScalarNode(tag='tag:yaml.org,2002:str', value='age'), ScalarNode(tag='tag:yaml.org,2002:int', value='24')), (ScalarNode(tag='tag:yaml.org,2002:str', value='password'), ScalarNode(tag='tag:yaml.org,2002:int', value='123456'))]))])
MappingNode(tag='tag:yaml.org,2002:map', value=[(ScalarNode(tag='tag:yaml.org,2002:str', value='user3'), MappingNode(tag='tag:yaml.org,2002:map', value=[(ScalarNode(tag='tag:yaml.org,2002:str', value='name'), ScalarNode(tag='tag:yaml.org,2002:str', value='xiaoli')), (ScalarNode(tag='tag:yaml.org,2002:str', value='age'), ScalarNode(tag='tag:yaml.org,2002:int', value='25')), (ScalarNode(tag='tag:yaml.org,2002:str', value='password'), ScalarNode(tag='tag:yaml.org,2002:int', value='123456'))]))])
2 yaml.load()
源码:
作用:解析流中的第一个
YAML
文档,并生成相应的Python
对象;解析:
# config_pyyaml.yaml如下:
user1:
name: xiaoming
age: 23
password: 123456
user2:
name: xiaozhang
age: 24
password: 123456
user3:
name: xiaoli
age: 25
password: 123456
# pyyaml_load.py如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28
# 文件名称:pyyaml_load.py
# 作用:yaml.load()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import yaml
with open('config_pyyaml.yaml', 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
print(data)
- 输出:
{'user1': {'name': 'xiaoming', 'age': 23, 'password': 123456}, 'user2': {'name': 'xiaozhang', 'age': 24, 'password': 123456}, 'user3': {'name': 'xiaoli', 'age': 25, 'password': 123456}}
3 yaml.load_all()
- 源码:
- 作用:解析流中的所有
YAML
文档并生成相应的Python
对象; - 解析:
# config_pyyaml01.yaml如下:
---
user1:
name: xiaoming
age: 23
password: 123456
---
user2:
name: xiaozhang
age: 24
password: 123456
---
user3:
name: xiaoli
age: 25
password: 123456
# pyyaml_load_all.py如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/7/28
# 文件名称:pyyaml_load_all.py
# 作用:yaml.load_all()
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import yaml
f = open('config_pyyaml01.yaml', 'r')
docs = yaml.load_all(f, Loader=yaml.FullLoader)
for doc in docs:
print(doc)
f.close()
- 输出:
{'user1': {'name': 'xiaoming', 'age': 23, 'password': 123456}}
{'user2': {'name': 'xiaozhang', 'age': 24, 'password': 123456}}
{'user3': {'name': 'xiaoli', 'age': 25, 'password': 123456}}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)