【python第三方库】python解析yaml文件内容

一、什么是yaml

Yaml文件:一种配置文件,类似于xml、json、ini等。

在自动化框架搭建中,一些配置文件(例如:数据库账号、url,日志级别等…)会经常的用到,所以我们需要一个存放这些参数的文件,并从中读取和写入。当然,存放参数除了用yaml,像ini文件、json文件、excel文件也是可以的,看个人选择。

  • Yaml文件结构简单,写法清晰,举个栗子:
network:
  name: abc
  type: internal
  gateway: 10.10.10.111
  • Yaml文件的写法有个特别要注意的地方:
  1. 同一级的字段要对齐
  2. 使用缩进表示不同的层级,但不允许用tab,使用的空格数量无所谓
  3. 冒号后面带上空格
  4. 区分大小写
  5. #表示注释
  • Yaml文件支持的数据类型如下:

对象: 键值对的集合

数组: 一组按次序排列的值

纯量: 单个的、不可再分的值

二、python如何解析

1. 安装

yaml 语法:http://www.ruanyifeng.com/blog/2016/07/yaml.html

pip install pyyaml

2. yaml样例

---
  UserName: Alicia
  Password: pinga123 * 
  phone: 3256
  TablesList:
        -EmployeeTable
        -SoftwaresList
        -HardwareList 
...

让我们理解一下这个yaml文件:

  • yaml文件以三个- 开始
  • 数据可以是任意类型,比如密码是string类型,电话是number类型
  • 缩进用于指示表列表内的项嵌套。里面的每个子项前面都有一个连字符。
  • yaml里面的注释用#表示
  • yaml 文件以…结尾,一个yaml文件可以包含多个yaml模块

3. load yaml文件

我们可以使用yaml.load()方法加载单个yaml. 这个方法会将yaml数据反序列化成python中的dict类型

# import pyyaml module
import yaml
from yaml.loader import SafeLoader

# Open the file and load the file
with open('Userdetails.yaml') as f:
    data = yaml.load(f, Loader=SafeLoader)
    print(data)

# 输出如下:
{'User': {'UserName': 'Alicia', 'Password': 'pinga123 *', 'phone': 3256, 'TablesList': ['haha', 'hhh']}}

对于load 方法有四种Loader参数:

  • BaseLoader: Loads all the basic YAML scalars as Strings
  • SafeLoader: Loads subset of the YAML safely, mainly used if the input is from an untrusted source.
  • FullLoader: Loads the full YAML but avoids arbitrary code execution. Still poses a potential risk when used for the untrusted input.
  • UnsafeLoader: Original loader for untrusted inputs and generally used for backward compatibility.

4. 使用 load_all() 加载多个yaml 数据

一个yaml文件可以包含多个document,每个单独的document以—开头,并以… 结尾。 我们可以一次性的读取所有数据通过使用 load_all 方法。load_all()函数解析给定的流并返回与流中的文档对应的Python对象序列。

---
User:
  UserName: Alicia
  Password: pinga123 * 
  phone: 3256
  TablesList:
    - haha
    - hhh
...
---
User:
  UserName: Alicia1
  Password: pinga123 * 
  phone: 3256
  TablesList:
    - haha
    - hhh
...

# import pyyaml module
import yaml
from yaml.loader import SafeLoader

# Open the file and load the file
with open('1.yaml') as f:
    data = list(yaml.load_all(f, Loader=SafeLoader))
    print(data)

5. 将python对象写入yaml

使用yaml.dump()方法去序列化一个可以转换为dictionary的python对象并保存为yaml

import yaml

# dict object
members = [{'name': 'Zoey', 'occupation': 'Doctor','hobby':[1,2,3,4,5]},
           {'name': 'Zaara', 'occupation': 'Dentist','hobby':[1,2,3,4,5]}]

# Convert Python dictionary into a YAML document
print(yaml.dump(members))

yaml.dump() 接收两个参数, data 以及stream. data是需要序列化保存到yaml的数据,第二个参数必须是一个代开的text或者二进制文件,如果你提供了第二个参数,yaml.dump()会将数据保存到文件

import yaml

user_details = {'UserName': 'Alice',
                'Password': 'star123*',
                'phone': 3256,
                'AccessKeys': ['EmployeeTable',
                               'SoftwaresList',
                               'HardwareList']}

with open('UserDetails.yaml', 'w') as f:
    data = yaml.dump(user_details, f, sort_keys=False, default_flow_style=False)

  • default_flow_style: This tag is used to display the contents of the nested blocks with proper indentation. The default value is True. In that case, the values inside the nested lists are shown in the flow style but setting this tag to False will display the block style’s contents with proper indentation.
  • sort_keys: This tag is used to sort the keys in alphabetical order. The default value is true. By setting the tag’s value as false we can maintain the insertion order.

6. 自定义的类序列化

Using the PyYAML module you can convert YAML into a custom Python object instead of a dictionary or built-in types. i.e., PyYAML allows you to read a YAML file into any custom Python object.

Also, You can dump instances of custom Python classes into YAML stream.

import yaml
from yaml.loader import UnsafeLoader

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __repr__(self):
        return "%s(name=%r, age=%r)" % (
            self.__class__.__name__, self.name, self.age)

# Make Python Class YAML Serializable
person = Person('Jessa', 28)
yaml_obj = yaml.dump(person)

# Deserialize YAML into a Custom Python Class
new_person = yaml.load(yaml_obj, Loader=UnsafeLoader)
print(new_person.name, new_person.age)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值