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: 1
dic2: '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.元祖

元祖语法和列表一样,只是加了个声明:

!!python/tuple # 列表转成元组声明语法
- name
- age
- class

# (1, 2, 3)

4.多组数据

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

--- !!python/tuple # 列表转成元组声明语法
- name
- age
- class
---
name: 111
age: 222
---
- 1
- 2

#<generator object load_all at 0x000001A1C8ED2820> 读取出来是个生成器对象,需要迭代取值
#('name', 'age', 'class')
#{'name': 111, 'age': 222}
#[1, 2]

二、yaml文件的读写

1.读取单组数据

import yaml #pip install pyyaml

with 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 pyyaml

with 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 yaml
def 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 yaml
def 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: 18
name: linux超
写入结果

参考:https://www.cnblogs.com/linuxchao/

 

posted @ 2021-12-23 16:30  www.pu  Views(315)  Comments(0Edit  收藏  举报