yaml 基础

yaml文件的作用

  • yaml是一种直观的能够被电脑识别的的数据序列化格式,容易被人类阅读,并且容易和脚本语言交互。

yaml的语法规则

  • 字母大小写敏感;
  • 通过缩进来表示层级关系,同层级元素需左对齐,且缩进的空格数多少没关系;
  • 缩进时不允许使用Tab,只允许使用空格
  • #表示注释

yaml支持字典和列表的表示

  • demo.yaml文件显示如下:
    test_list:
    - name
    - age
    - weight
    test_dict:
    name: jack
    age: 18
    通过Python如下语句读取:
import yaml

with open(demo.yaml,'rb') as f:
    config = yaml.load(f)
test_list = config["test_list"]
test_dict = config["tesst_dict"]
  • 其中test_list返回一个list,test_dict返回dict,俩者也可相互组合

参考:https://blog.csdn.net/lmj19851117/article/details/7843486

原文地址:https://www.cnblogs.com/ronky/p/9231675.html