python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python使用yaml模块

Python使用yaml模块操作YAML文档的方法

作者:牛油菠蘿包

YAML是可读性高,用来表达数据序列化格式的,专用于写配置文件的语言,这篇文章主要介绍了Python使用yaml模块操作YAML文档,需要的朋友可以参考下

1. YAML简介

YAML是可读性高,用来表达数据序列化格式的,专用于写配置文件的语言。YAML文件其实也是一种配置文件类型,后缀名是.yaml或.yml都可以。其以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁。

2. 语法规则

3. 文件数据结构

对象:键值对的集合(简称"映射或字典")

键值对用冒号 “:” 结构表示 冒号与值之间需用空格分隔

数组:一组按序排列的值(简称"序列或列表")

数组前加有 “-” 符号 符号与值之间需用空格分隔

纯量(scalars):单个的、不可再份的值(如:字符串、bool值、整数、浮点数、时间、日期、null等)

None值可用null,也可用~表示

4. YAML数据格式示例

# 对象:yaml键值对;即Python中字典
user: 'admin'
pwd: 'admin@123'
site: "www.yaml.com"
# 解析后: {'user': 'admin', 'password': 'admin@123', 'site': 'www.yaml.com'}

# 2. 数组:yaml键值对中嵌套数组
user2:
 - a
 - b
 - c
user3:
 - d
# 解析后:{'user2':['a','b','c'],'user3':['d']}

# 3. 纯量
val_name: name      # 字符串: {'val_name': 'name'}
spec_val: "name\n" # 特殊字符串: {'spec_val': 'name\n'}
pi_val: 3.14        # 数字: {'pi_val': 3.14}
bol_val: true       # 布尔值: {'bol_val': true}
nul_val: null       # null值: {'nul_val': None}
nul_val: ~          # null值: {'nul_val': None}
time_val: 2023-02-03t22:33:22.33-03:00      # 时间值:{'time_val': datetime.datetime(2023, 2, 3, 22, 33, 22, 330000)}
date_val: 2024-01-01        # 日期值:{'date_val': datetime.date(2024, 1, 1)}

# 4. 引用
name: &name 白云
tester: *name
# 相当于
name: 白云
tester: 白云
# 解析后内容:{'name': '白云', 'tester': '白云'}

# 5. 强制转换
str: !!str 3.14
int: !!int "666"
# 输出: {'str': '3.14','int': 123}

5. 安装yaml库

pip install pyyaml

6. 读取YAML

6.1 读取键值对或嵌套键值对

yaml文件内容为:

user1:
 name: xm
 stu: 101
user2:
 name: xh
 stu: 102
user3:
 name: xl
 stu: 103

程序代码:

import yaml
import os
class ReadYAML(object):
    def read_yaml(self,yaml_file):
        with open(yaml_file,'r',encoding='utf-8') as f:
            file_data = f.read()
            print("file_data类型:",type(file_data))
            data = yaml.safe_load(file_data)
            print("data类型:",type(data))
        
        return data



if __name__ == "__main__":
    base_name = os.path.dirname(os.path.realpath(__file__))
    yaml_path = os.path.join(base_name,'test.yaml')
    ry = ReadYAML()
    res = ry.read_yaml(yaml_path)
    print(res)

输出结果:

file_data类型: <class 'str'>
data类型: <class 'dict'>
{'user1': {'name': 'xm', 'stu': 101}, 'user2': {'name': 'xh', 'stu': 102}, 'user3': {'name': 'xl', 'stu': 103}}

6.2 读取数组类型

yaml文件内容为:

class1:
 - stu1
 - stu2
 - stu3
class2:
 - stu2

程序代码:

import yaml
import os
class ReadArraysYAML(object):
    def read_yaml(self,yaml_file):
        with open(yaml_file,'r',encoding='utf-8') as f:
            file_data = f.read()
            # print("file_data类型:",type(file_data))
            data = yaml.safe_load(file_data)
            # print("data类型:",type(data))
        
        return data


if __name__ == "__main__":
    base_name = os.path.dirname(os.path.realpath(__file__))
    yaml_path = os.path.join(base_name,'arrays.yaml')
    ry = ReadArraysYAML()
    res = ry.read_yaml(yaml_path)
    print(res)

输出结果:

{'class1': ['stu1', 'stu2', 'stu3'], 'class2': ['stu2']}

6.3 多文档同在一份yaml文件中时的读取方法

yaml文件内容:

# 分段yaml文件中存在多个文档
---
animal1: dog
age: 1
---
animal2: cat
age: 2

程序代码:

"""
多文档同在一份yaml文件中时的读取方法(使用yaml.safe_load_all())
"""
import yaml
import os
def get_yaml_load_all(yaml_file):
    file = open(yaml_file,'r',encoding='utf-8')
    file_data = file.read()
    file.close()
    all_data = yaml.safe_load_all(file_data)
    for data in all_data:
        print(data)
if __name__ == "__main__":
    current_path = os.path.dirname(__file__)
    print(current_path)
    yaml_path = os.path.join(current_path,'muti.yaml')
    get_yaml_load_all(yaml_path)

输出结果:

d:\PyProject\YAML
{'animal1': 'dog', 'age': 1}
{'animal2': 'cat', 'age': 2}

6.4 向YAML文档写入

程序代码:

"""
使用yaml.dump()方法将列表或字典数据写入进已存在的yaml文档
"""
import yaml
import os
def generate_yaml_doc(yaml_file):
    py_object = {'school':'Fxxking U','student':['stu1','stu2']}
    file = open(yaml_file,'w',encoding='utf-8')
    yaml.safe_dump(py_object,file)
    file.close()
if __name__ == "__main__":
    current_path = os.path.dirname(__file__)
    print(current_path)
    yaml_path = os.path.join(current_path,'generateYAML.yaml')
    generate_yaml_doc(yaml_path)

写入后,YAML文档内容:

school: Fxxking U
student:
- stu1
- stu2

注:若想要以追加的形式写入,只需将open()中的’w’改为’a’即可

6.5 更新/修改 YAML文件内容

修改前YAML文件内容:

school: Fxxking U
student:
- stu1
- stu2

程序代码:

import yaml
import os
from readArraysYAML import ReadArraysYAML

def update_yaml(k,v,yaml_file):
    readY = ReadArraysYAML()
    old_data = readY.read_yaml(yaml_file)
    old_data[k] = v     # 修改读取的数据,如果k不存在则新增一组键值对
    with open(yaml_file,'w',encoding='utf-8') as f:
        yaml.safe_dump(old_data,f)

if __name__ == "__main__":
    current_path = os.path.dirname(__file__)
    yaml_path = os.path.join(current_path,'generateYAML.yaml')
    k = 'school'
    v = 'SZ U'
    update_yaml(k,v,yaml_path)

修改后,YAML文件内容:

school: SZ U
student:
- stu1
- stu2

7. 使用ruamel模块将数据转换为标准的yaml内容

安装ruamel库

pip install ruamel.yaml

程序代码:

from ruamel import yaml
import os
def generate_yaml_doc_ruamel(yaml_file):
    py_object = {'file_type':'ruamel_yaml','school':'Fxxking U','student':['c','d']}
    with open(yaml_file,'w',encoding='utf-8') as f:
        yaml.dump(py_object,f,Dumper=yaml.RoundTripDumper)
if __name__ == "__main__":
    current_path = os.path.dirname(__file__)
    yaml_path = os.path.join(current_path,'ruamelGenerateYAML.yaml')
    generate_yaml_doc_ruamel(yaml_path)
    print("写入成功!")

写入后,YAML文件内容:

file_type: ruamel_yaml
school: Fxxking U
student:
- c
- d

到此这篇关于Python使用yaml模块操作YAML文档的文章就介绍到这了,更多相关Python使用yaml模块内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文