原文链接: Python yaml
YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写。它实质上是一种通用的数据串行化格式。
它的基本语法规则如下:
1、大小写敏感
2、使用缩进表示层级关系
3、缩进时不允许使用Tab键,只允许使用空格。
4、缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
5、# 表示注释,从这个字符一直到行尾,都会被解析器忽略,这个和python的注释一样
YAML 支持的数据结构有三种:
1、对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
2、数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
3、纯量(scalars):单个的、不可再分的值。字符串、布尔值、整数、浮点数、Null、时间、日期
基本读写操作
1# FileName : YamlDemo.py 2# Author : Adil 3# DateTime : 2017/12/29 12:00 4# SoftWare : PyCharm 5 6import yaml 7 8# 获取配置文件的路径 D:/WorkSpace/StudyPractice/Python_Yaml/YamlStudy\config.yaml 9# 加上 ,encoding='utf-8',处理配置文件中含中文出现乱码的情况。 10 11yamlPath = 't.yaml' 12with open(yamlPath, 'r', encoding='utf-8')as f: 13 cont = f.read() 14# 载入配置信息,返回的是类似json的dict格式 15x = yaml.load(cont) 16 17# print(type(x)) 18print(x) 19 20# 写入yaml 文件 21 22 23# 构建数据 24data = {"cookie1": {'domain': '.yiyao.cc', 'expiry': 1521558688.480118, 'httpOnly': False, 'name': '_ui_', 'path': '/', 25 'secure': False, 'value': 'HSX9fJjjCIImOJoPUkv/QA=='}} 26# a 追加写入,w,覆盖写入 27with open(yamlPath, 'w+', encoding='utf-8') as f: 28 # 装载数据 29 yaml.dump(data, f) 30 31# 读取数据,获取文件 32with open(yamlPath, 'r', encoding='utf-8') as f: 33 # 读取文件 34 cont = f.read() 35 # 加载数据 36 x = yaml.load(cont) 37 38# 打印数据 39print(x) 40# 打印读取写入的数据 41 42 43#######################################字符串############################################## 44#1、字符串默认不使用引号表示 45str1: 这是一个字符串 46 47#2、如果字符串之中包含空格或特殊字符,需要放在引号之中。 48str2: '内容: *字符串' 49 50#3、单引号和双引号都可以使用,双引号不会对特殊字符转义。 51str3: '内容\n字符串' 52str4: "content\n string" 53 54#4、单引号之中如果还有单引号,必须连续使用两个单引号转义。 55s3: 'labor''s day' 56 57#5、字符串可以写成多行,从第二行开始,必须有一个单空格缩进。换行符会被转为空格 58strline: 这是一段 59 多行 60 字符串 61 62#6、多行字符串可以使用|保留换行符,也可以使用>折叠换行 63this: | 64 Foo 65 Bar 66that: > 67 Foo 68 Bar 69 70#7、+表示保留文字块末尾的换行,-表示删除字符串末尾的换行。 71s4: | 72 Foo4 73s5: |+ 74 Foo5 75s6: |- 76 Foo6 77s7: | 78 Foo7 79 80#1、对象的一组键值对,使用冒号结构表示。 81animal: pets #{'animal': 'pets'} 82# 83##2、Yaml 也允许另一种写法,将所有键值对写成一个行内对象 84dict1: { name: Steve, foo: bar } #{'dict1': {'foo': 'bar', 'name': 'Steve'}} 85 86####################################数组################### 87 88# 1、数组可以采用行内表示法。 89animal: [Cat, Dog] 90 91#{'animal': ['Cat', 'Dog']} 92 93#2、一组连词线开头的行,构成一个数组。 94animal1: 95 - Cat 96 - Dog 97 - Goldfish 98 99# {'animal1': ['Cat', 'Dog', 'Goldfish']} 100 101############################复合结构########################## 102#对象和数组可以结合使用,形成复合结构 103 104languages: 105 - Ruby 106 - Perl 107 - Python 108websites: 109 YAML: yaml.org 110 Ruby: ruby-lang.org 111 Python: python.org 112 Perl: use.perl.org 113#{'languages': ['Ruby', 'Perl', 'Python'], 'websites': {'Python': 'python.org', 'YAML': 'yaml.org', 'Ruby': 'ruby-lang.org', 'Perl': 'use.perl.org'}} 114 115db: 116 host: xxx 117 port: 3306 118 user: weibospider 119 password: xxx 120 db_name: weibo 121 db_type: mysql 122 123#{'db': {'host': 'xxx', 'db_name': 'weibo', 'user': 'weibospider', 'db_type': 'mysql', 'password': 'xxx', 'port': 3306}} 124 125##########################纯量############################# 126#1、数值直接以字面量的形式表示 127number: 12.30 #{'number': 12.3} 128 129#2、布尔值用true和false表示 130isSet: true #{'isSet': True} 131isSet1: false #{'isSet1': False} 132 1333、null用~表示 134parent: ~ #{'parent': None} 135 136#4、时间采用 ISO8601 格式。 137time1: 2001-12-14t21:59:43.10-05:00 #{'time1': datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)} 138 139##5、日期采用复合 iso8601 格式的年、月、日表示。 140date: 2017-07-31 #{'date': datetime.date(2017, 7, 31)} 141 142#6、YAML 允许使用两个感叹号,强制转换数据类型。 143int_to_str: !!str 123 #{'bool_to_str': 'true'} 144bool_to_str: !!str true #{'bool_to_str': 'true'}