YAML语法

YAML

取材自

简介

YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。

它的基本语法规则如下。

  • 大小写敏感
  • 使用缩进表示层级关系
    • 缩进时不允许使用Tab键,只允许使用空格。
    • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可。
  • 字符串无需引号(""''
    • 含特殊字符或空格需使用引号
  • # 表示注释,从这个字符一直到行尾,都会被解析器忽略。

YAML 支持的数据结构有三种。

  • 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
  • 纯量(scalars):单个的、不可再分的值

数据结构

对象

{"animal": "dog"}

{"animal": {"dog":"Corgi", "cat":"Ragdoll"}}
animal: dog

animal:
  dog: Corgi
  cat: Ragdoll
animal: {dog: Corgi, cat: Ragdoll}

数组

["Corgi", "Ragdoll"]

[["Corgi", "Ragdoll"]]

{"animal": ["Corgi", "Ragdoll"]}
- Corgi
- Ragdoll

-
  - Corgi
  - Ragdoll
  
animal:
  - Corgi
  - Ragdoll

animal: [Corgi, Ragdoll]

复合结构

{ "languages": [ "Ruby", "Perl", "Python" ],
  "websites": 
   { "YAML": "yaml.org",
     "Ruby": "ruby-lang.org",
     "Python": "python.org",
     "Perl": "use.perl.org"
   }
}
languages:
  - Ruby
  - Perl
  - Python
websites:
  YAML: yaml.org
  Ruby: ruby-lang.org
  Python: python.org
  Perl: use.perl.org

纯量

字符串
布尔值
{ "use_gpu":true, 
  "use_se":false
}
use_gpu: true
use_se: false
整数


浮点数
Null
{ "parent": null
}
parent: ~
时间
日期
强制转换数据类型
{ number_str: "123",
  boolean_str: "true"
}
number_str: !!str 123
boolean_str: !!str true 

引用

YAML与Python

YAML -> Python

load
# config.yml
name: Tom Smith
age: 37
spouse:
    name: Jane Smith
    age: 25
children:
 - name: Jimmy Smith
   age: 15
 - name1: Jenny Smith
   age1: 12
# test_config.py
import yaml
f = open(r'E:\AutomaticTest\Test_Framework\config\config.yml')
y = yaml.load(f)
print (y)
# {'name': 'Tom Smith', 'age': 37, 'spouse': {'name': 'Jane Smith', 'age': 25}, 'children': [{'name': 'Jimmy Smith', 'age': 15}, {'name1': 'Jenny Smith', 'age1': 12}]}
load_all
# test_config.py
import yaml
f = '''
---
name: James
age: 20
---
name: Lily
age: 19
'''
y = yaml.load_all(f)
for data in y:
    print(data)
# {'name': 'James', 'age': 20}
# {'name': 'Lily', 'age': 19}

Python -> YAML

dump
# test_config.py
import yaml
a_project = { 'name': 'Silenthand Olleander',
              'race': 'Human',
              'traits': ['ONE_HAND', 'ONE_EYE']
            }

print(yaml.dump(aproject,))

# name: Silenthand Olleander
# race: Human
# traits: [ONE_HAND, ONE_EYE]
  • yaml.dump接收的第二个参数一定要是一个打开的文本文件或二进制文件,yaml.dump会把生成的yaml文档写到文件里
# test_config.py
import yaml

aproject = {'name': 'Silenthand Olleander',
            'race': 'Human',
            'traits': ['ONE_HAND', 'ONE_EYE']
            }
f = open(r'E:\AutomaticTest\Test_Framework\config\config.yml','w')
yaml.dump(aproject,f)
dump_all

将多个段输出到一个文件中

import yaml

obj1 = {"name": "James", "age": 20}
obj2 = ["Lily", 19]

with open(r'E:\AutomaticTest\Test_Framework\config\config.yml', 'w') as f:
    yaml.dump_all([obj1, obj2], f)
{age: 20, name: James}
--- [Lily, 19]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值