yaml文件的读写(yaml文件详解)

网友投稿 1280 2022-09-01


yaml文件的读写(yaml文件详解)

一、yaml文件基本语法

yaml文件其实也是一种配置文件类型,相比较ini,conf,py配置文件来说,更加的简洁,操作也更加简单,同时可以存放不同类型的数据,不会改变原有数据类型,所有的数据类型在读取时都会原样输出

安装:

pip install pyyaml

1.列表

列表类型书写主要是 - 和 空格 的结合,不同的缩进表示层级,如下:

- 1- '2'- 3 - 4- # 嵌套列表的话就一个-加一个空格 - 5 # - ''- - - 6 # 这表示嵌套两层列表 - 7 --- id: 11 # 列表套字典 name: 22 like: 33#结果: [1, '2', '3 - 4', [5, ''], [[6, 7, None]], None, {'id': 11, 'name': 22, 'like': 33}]

列表语法

2.字典

列表类型书写语法主要是  冒号,不同的缩进表示层级,如下:

dic1: 1dic2: '2'dic3: [666,777,888] # 也支持列表dic4: {'11':22,'22':'33'} # 支持字典dict5: # 字典嵌套 name: 'xx' pwd: 'oo' like: # 字典套列表 - girl - ball - xxoo: 'ooxx' # 再嵌套列表 ooxx: xxoo#{'dic1': 1,#'dic2': '2',#'dic3': [666, 777, 888],#'dic4': {'11': 22, '22': '33'},#'dict5': {'name': 'xx', 'pwd': 'oo', 'like': ['girl', 'ball', {'xxoo': 'ooxx', 'ooxx': 'xxoo'}]}#}

字典语法

3.元祖

4.多组数据

多组数据的分隔语法是 ---

二、yaml文件的读写

1.读取单组数据

import yaml #pip install pyyamlwith open('_tuple.yaml', encoding='utf-8') as f: # res = yaml.load(f,Loader=yaml.FullLoader) res = yaml.full_load(f) # 上面的简便写法 print(res) print(type(res)) for line in res: print(line)

2.读取多组数据

import yaml #pip install pyyamlwith open('_tuple.yaml', encoding='utf-8') as f: res = yaml.load_all(f,Loader=yaml.FullLoader) res = yaml.full_load_all(f) # 上面的简便写法,返回的是一个生成器对象,需要迭代取值 print(res) print(type(res)) for line in res: print(line)

3.写单组数据

response = { "status": 1, "code": "1001", "data": [ { "id": 80, "regname": "toml", "pwd": "QW&@JBK!#&#($*@HLNN", "mobilephone": "13691579846", "leavemount": "0.00", "type": "1", "regtime": "2019-08-14 20:24:45.0" }, { "id": 81, "regname": "toml", "pwd": "QW&@JBK!#&#($*@HLNN", "mobilephone": "13691579846", "leavemount": "0.00", "type": "1", "regtime": "2019-08-14 20:24:45.0" } ], "msg": "获取用户列表成功"}

response

import yamldef write_yaml(): with open("config.yaml", encoding='utf-8',mode='w') as f: try: yaml.dump(data=response,stream=f,allow_unicode=True) except Exception as e: print(e)write_yaml()

ode: '1001'data:- id: 80 leavemount: '0.00' mobilephone: '13691579846' pwd: QW&@JBK!#&#($*@HLNN regname: toml regtime: '2019-08-14 20:24:45.0' type: '1'- id: 81 leavemount: '0.00' mobilephone: '13691579846' pwd: QW&@JBK!#&#($*@HLNN regname: toml regtime: '2019-08-14 20:24:45.0' type: '1'msg: 获取用户列表成功status: 1

结果config.yaml

4.写多组数据

response = { "status": 1, "code": "1001", "data": [ { "id": 80, "regname": "toml", "pwd": "QW&@JBK!#&#($*@HLNN", "mobilephone": "13691579846", "leavemount": "0.00", "type": "1", "regtime": "2019-08-14 20:24:45.0" }, { "id": 81, "regname": "toml", "pwd": "QW&@JBK!#&#($*@HLNN", "mobilephone": "13691579846", "leavemount": "0.00", "type": "1", "regtime": "2019-08-14 20:24:45.0" } ], "msg": "获取用户列表成功"}info = { "name": "linux超", "age": 18}

要写入的多组数据

import yamldef write_yaml(): with open("config.yaml", encoding='utf-8',mode='w') as f: try: yaml.dump_all(documents=[response,info],stream=f,allow_unicode=True) except Exception as e: print(e)write_yaml()

写入后的config.yaml为code: '1001'data:- id: 80 leavemount: '0.00' mobilephone: '13691579846' pwd: QW&@JBK!#&#($*@HLNN regname: toml regtime: '2019-08-14 20:24:45.0' type: '1'- id: 81 leavemount: '0.00' mobilephone: '13691579846' pwd: QW&@JBK!#&#($*@HLNN regname: toml regtime: '2019-08-14 20:24:45.0' type: '1'msg: 获取用户列表成功status: 1---age: 18name: linux超

写入结果


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java利用沙箱支付实现电脑扫码支付教程
下一篇:unittest单元测试框架基础知识(unittest测试框架的使用流程)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~