https://www.jianshu.com/p/eaa1bf01b3a6
yaml是一个专门用来写配置文件的语言。
python中读取yaml文件前需要安装pyyaml和导入yaml模块:
使用yaml需要安装的模块为pyyaml(pip3 install pyyaml
);
导入的模块为yaml(import yaml
)
python通过open方式读取文件数据,再通过load函数将数据转化为列表或字典;
import yaml
import os
def get_yaml_data(yaml_file):
# 打开yaml文件
print("***获取yaml文件数据***")
file = open(yaml_file, 'r', encoding="utf-8")
file_data = file.read()
file.close()
print(file_data)
print("类型:", type(file_data))
# 将字符串转化为字典或列表
print("***转化yaml数据为字典或列表***")
data = yaml.load(file_data)
print(data)
print("类型:", type(data))
return data
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "config.yaml")
get_yaml_data(yaml_path)
"""
***获取yaml文件数据***
# yaml键值对:即python中字典
usr: my
psw: 123455
类型:<class 'str'>
***转化yaml数据为字典或列表***
{'usr': 'my', 'psw': 123455}
类型:<class 'dict'>
"""
# yaml键值对:即python中字典
usr: my
psw: 123455
s: " abc\n"
python解析yaml文件后获取的数据:
{'usr': 'my', 'psw': 123455, 's': ' abc\n'}
# yaml键值对嵌套:即python中字典嵌套字典
usr1:
name: a
psw: 123
usr2:
name: b
psw: 456
python解析yaml文件后获取的数据:
{'usr1': {'name': 'a', 'psw': 123}, 'usr2': {'name': 'b', 'psw': 456}}
# yaml键值对中嵌套数组
usr3:
- a
- b
- c
usr4:
- b
python解析yaml文件后获取的数据:
{'usr3': ['a', 'b', 'c'], 'usr4': ['b']}
# yaml数组
- a
- b
- 5
['a', 'b', 5]
# yaml"数组"中嵌套"键值对"
- usr1: aaa
- psw1: 111
usr2: bbb
psw2: 222
[{'usr1': 'aaa'}, {'psw1': 111, 'usr2': 'bbb', 'psw2': 222}]
# 纯量
s_val: name # 字符串:{'s_val': 'name'}
spec_s_val: "name\n" # 特殊字符串:{'spec_s_val': 'name\n'
num_val: 31.14 # 数字:{'num_val': 31.14}
bol_val: true # 布尔值:{'bol_val': True}
nul_val: null # null值:{'nul_val': None}
nul_val1: ~ # null值:{'nul_val1': None}
time_val: 2018-03-01t11:33:22.55-06:00 # 时间值:{'time_val': datetime.datetime(2018, 3, 1, 17, 33, 22, 550000)}
date_val: 2019-01-10 # 日期值:{'date_val': datetime.date(2019, 1, 10)}
yaml文件中内容
animal3: &animal3 fish
test: *animal3
python读取的数据
{'animal3': 'fish', 'test': 'fish'}
如:yaml文件中数据
# 分段yaml文件中多个文档
---
animal1: dog
age: 2
---
animal2: cat
age: 3
python获取yaml数据时需使用load_all函数来解析全部的文档,再从中读取对象中的数据
# yaml文件中含有多个文档时,分别获取文档中数据
def get_yaml_load_all(yaml_file):
# 打开yaml文件
file = open(yaml_file, 'r', encoding="utf-8")
file_data = file.read()
file.close()
all_data = yaml.load_all(file_data)
for data in all_data:
print(data)
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "config.yaml")
get_yaml_load_all(yaml_path)
"""结果
{'animal1': 'dog', 'age': 2}
{'animal2': 'cat', 'age': 3}
"""
通过yaml.dump()方法不会将列表或字典数据进行转化yaml标准模式,只会将数据生成到yaml文档中
# 将python对象生成yaml文档
import yaml
def generate_yaml_doc(yaml_file):
py_object = {'school': 'zhang',
'students': ['a', 'b']}
file = open(yaml_file, 'w', encoding='utf-8')
yaml.dump(py_object, file)
file.close()
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "generate.yaml")
generate_yaml_doc(yaml_path)
"""结果
school: zhang
students: [a, b]
"""
(1)使用ruamel模块中yaml前提条件
pip3 install ruamel.yaml
)from ruamel import yaml
(2)ruamel模块生成yaml文档
def generate_yaml_doc_ruamel(yaml_file):
from ruamel import yaml
py_object = {'school': 'zhang',
'students': ['a', 'b']}
file = open(yaml_file, 'w', encoding='utf-8')
yaml.dump(py_object, file, Dumper=yaml.RoundTripDumper)
file.close()
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "generate.yaml")
generate_yaml_doc_ruamel(yaml_path)
"""结果
school: zhang
students:
- a
- b
"""
(3)ruamel模块读取yaml文档
# 通过from ruamel import yaml读取yaml文件
def get_yaml_data_ruamel(yaml_file):
from ruamel import yaml
file = open(yaml_file, 'r', encoding='utf-8')
data = yaml.load(file.read(), Loader=yaml.Loader)
file.close()
print(data)
current_path = os.path.abspath(".")
yaml_path = os.path.join(current_path, "dict_config.yaml")
get_yaml_data_ruamel(yaml_path)