python 解析yaml文件

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

  2. 安装

pip install pyyaml
  1. yaml 样例
---
  UserName: Alicia
  Password: pinga123 * 
  phone: 3256
  TablesList:
        -EmployeeTable
        -SoftwaresList
        -HardwareList 
...

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

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

  1. 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.

  1. 使用 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)
  1. 将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.

  1. 自定义的类序列化

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)

https://pynative.com/python-yaml/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值