本文介绍如何读写操作yaml文件
首先我们需要下载第三方包:PyYAML(下载方法:打开cmd,输入pip install 包名)
写个类封装yaml文件的读写方法
代码如下:
import yaml #导入yaml模块
#封装一个类方法
class RWyaml(object):
def __init__(self, file_name):
self.y = yaml
self.file = file_name #yaml文件绝对路径
#字典形式读取所有内容
def read_yaml_all(self):
with open(self.file, 'r', encoding='utf-8') as f:
return self.y.load(f, Loader=self.y.FullLoader)
#写入(完善了下,可修改key值)
def write_yaml(self, nb, key, value):
data = {nb: {key: value}}
old_data = self.read_yaml_all()
if nb in old_data:
old_data[nb][key] = value
with open(self.file, 'w', encoding='utf-8') as f:
self.y.dump(old_data, f)
print('写入成功:%s' % old_data)
else:
with open(self.file, 'a', encoding='utf-8') as f:
self.y.dump(data, f)
print('写入成功:%s' % data)
#读取节点下面key的值
def read_yaml_value(self, nb, key):
with open(self.file, 'r', encoding='utf-8') as f:
data = self.y.load(f, Loader=self.y.FullLoader)
try:
if nb in data.keys():
return data[nb][key]
else:
print('节点nb:%s不存在!' % nb)
except KeyError as e:
print('key:%s不存在!' % key)
if __name__=='__main__':
#执行
yaml = RWyaml('test.yaml')
yaml.write_yaml('nb2', 'name', 'Tom')
print(yaml.read_yaml_value('nb1', 'name'))
print(yaml.read_yaml_all())
执行结果如下:
写入成功:{'nb2': {'name': 'Tom', 'age': 18}}
{'nb1': {'age': 16, 'name': 'Sonny'}, 'nb2': {'age': 18, 'name': 'Tom'}}
Sonny
稍适修改,中文写入为ASC码,不便观看,所有使用英文key。
再加上yaml文件如下:
nb1:
age: 16
name: Sonny
nb2:
age: 18
name: Tom