Python基础笔记2-ruamel.yaml读写yaml文件

简介: 上一篇笔记记录了Python中的pyyaml库对yaml文件进行读写,但了解到ruamel.yaml也能对yaml文件进行读写,于是想尝试一下它的用法。


一,注意


这里首先要更正一下网上大部分博客的说法:使用PyYAML写入时不是yaml的标准形式。例如使用PyYAML将字典嵌套字典的数据写入yaml文件时,写入的yaml文件里会出现带{}的数据。实际我在写代码的过程中发现PyYAML5.3.1版本并不会出现这种情况。如下所示:


使用PyYAML库写入yaml文件

# @author: 给你一页白纸
import yaml
data = {
   "str": "Hello world.",
   "int": 110,
   "list": [10, "she", ["he", "it"]],
   "dict": {"account":"xiaoqq", "password": {"pwd1": 123456, "pwd2": "water"}},
   "tuple": (100, "a")
}
with open('./writeYamlData.yml', 'w', encoding='utf-8') as f:
   yaml.dump(data=data, stream=f, allow_unicode=True)

写入后的yaml文件内容如下:

dict:
  account: xiaoqq
  password:
    pwd1: 123456
    pwd2: water
int: 110
list:
- 10
- she
- - he
  - it
str: Hello world.
tuple: !!python/tuple
- 100
- a


二,安装ruamel.yaml库


安装命令:

pip install ruamel.yaml
# 安装速度慢则加上镜像源
pip install ruamel.yaml -i https://pypi.tuna.tsinghua.edu.cn/simple


使用时导入:

from ruamel import yaml


三,ruamel.yaml写入yaml文件


使用yaml.dump()方法写入,代码展示如下:

# @author: 给你一页白纸
from ruamel import yaml
data = {
   "str": "Hello world.",
   "int": 110,
   "list": [10, "she", ["he", "it"]],
   "dict": {"account":"xiaoqq", "password": {"pwd1": 123456, "pwd2": "water"}},
   "tuple": (100, "a")
}
# path为yaml文件的绝对路径
path ='./writeYamlData.yml'
with open(path, 'w', encoding='utf-8') as f:
   yaml.dump(data, f, Dumper=yaml.RoundTripDumper)


写入结果如下:

str: Hello world.
int: 110
list:
- 10
- she
- - he
  - it
dict:
  account: xiaoqq
  password:
    pwd1: 123456
    pwd2: water
tuple:
- 100
- a


注意:这里yaml.dump()里加上l了参数Dumper=yaml.RoundTripDumper。不加该参数则写入结果如下:

dict:
  account: xiaoqq
  password: {pwd1: 123456, pwd2: water}
int: 110
list:
- 10
- she
- [he, it]
str: Hello world.
tuple: !!python/tuple [100, a]


四,ruamel.yaml读取yaml文件


yaml文件数据如下:

dict:
  account: xiaoqq
  password:
    pwd1: 123456
    pwd2: water
int: 110
list:
- 10
- she
- - he
  - it
str: Hello world.
tuple: !!python/tuple
- 100
- a


使用yaml.load()方法读取,代码展示如下:

# @author: 给你一页白纸
from ruamel import yaml
# path为yaml文件的绝对路径
path ='./writeYamlData.y'
with open(path, 'r', encoding='utf-8') as doc:
    content = yaml.load(doc, Loader=yaml.Loader)
    print(content)


读取结果如下:

C:\Users\xiaoqq\AppData\Local\Programs\Python\Python37\python.exe C:/Users/xiaoqq/Desktop/test_project/readYaml.py
{'dict': {'account': 'xiaoqq', 'password': {'pwd1': 123456, 'pwd2': 'water'}}, 'int': 110, 'list': [10, 'she', ['he', 'it']], 'str': 'Hello world.', 'tuple': (100, 'a')}
Process finished with exit code 0


ruamel.yaml库继承子PyMYAL库,读写方法基本相同,目前来说可以根据自己的习惯选择使用 ruamel.yaml 还是 PyMYAL 进行yaml文件的读写操作。

相关文章
|
6天前
|
JSON Kubernetes API
深入理解Kubernetes配置:编写高效的YAML文件
深入理解Kubernetes配置:编写高效的YAML文件
|
15天前
|
数据挖掘 Python
🚀告别繁琐!Python I/O管理实战,文件读写效率飙升的秘密
在日常编程中,高效的文件I/O管理对提升程序性能至关重要。Python通过内置的`open`函数及丰富的库简化了文件读写操作。本文从基本的文件读写入手,介绍了使用`with`语句自动管理文件、批量读写以减少I/O次数、调整缓冲区大小、选择合适编码格式以及利用第三方库(如pandas和numpy)等技巧,帮助你显著提升文件处理效率,让编程工作更加高效便捷。
30 0
|
3天前
|
Python
Python 解析 yaml 配置文件
Python 解析 yaml 配置文件
10 0
|
6天前
|
Python
7-9|Python如何安装ruamel.yaml
7-9|Python如何安装ruamel.yaml
|
2月前
|
SQL JSON 关系型数据库
n种方式教你用python读写excel等数据文件
n种方式教你用python读写excel等数据文件
|
2月前
|
机器学习/深度学习 计算机视觉 Python
深度学习项目中在yaml文件中定义配置,以及使用的python的PyYAML库包读取解析yaml配置文件
深度学习项目中在yaml文件中定义配置,以及使用的python的PyYAML库包读取解析yaml配置文件
41 0
|
2月前
|
JSON Kubernetes 数据格式
k8s集群yaml文件方式迁移
k8s集群yaml文件方式迁移
|
2月前
|
Kubernetes API 容器
在K8S中,deployment的yaml文件如何编写呢?
在K8S中,deployment的yaml文件如何编写呢?
|
2月前
|
Python
【python】】Python 的 queue 模块使用笔记
【python】】Python 的 queue 模块使用笔记
28 0
|
2月前
|
Python
Python:读写操作
【8月更文挑战第20天】
30 0
下一篇
无影云桌面