Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe5cafc

Browse files
committed
add yaml and json
Change-Id: I1571a450d67fc5dc1ddec7aa554114fc4435393b
1 parent c732001 commit fe5cafc

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ python的强大之处有很大的一方面在于它有各种各样非常强大
6161

6262
## [json](content/json.md)
6363

64+
## [yaml](content/yaml.md)
65+
6466
## [time](content/time.md)
6567

6668
## [datetime](content/datetime.md)

‎code/yaml_file_demo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
import yaml
3+
4+
5+
if __name__ == '__main__':
6+
data = {
7+
'name': 'ACME',
8+
'object': {'complex': True, 'description': 'complex object'},
9+
'shares': 100,
10+
'price': 542.23,
11+
'others': ["first thing", "second thing", "third thing"],
12+
}
13+
with open('yaml_test.yaml', 'w') as f:
14+
raw_yaml_data = yaml.safe_dump(data, f)
15+
16+
with open('yaml_test.yaml', 'r') as f:
17+
print(yaml.safe_load(f))

‎content/yaml.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## yaml
2+
3+
yaml 和 json 是数据格式化的两种不同方式,json 更加人性化,更加易读一些,个人比较喜欢 json。
4+
5+
但是 yaml 功能更强大,而且 yaml 是 json 的超集,每个 json 文件都是合法的 yaml 文件,也就是说,市面上任何一个标准的 yaml 解析器,都是可以无缝兼容 json 格式的。
6+
> every JSON file is also a valid YAML file, reference from [Relation to JSON](https://yaml.org/spec/1.2/spec.html#id2759572)
7+
8+
## 与 json 的差异
9+
10+
主要从四个方面来比较一下,解析速度,内存占用,表现力,可移植性
11+
12+
### 解析速度
13+
解析速度取决于解析器的实现,一般来说,json 的使用和实现都比 yaml 要多,也就是说能够取得更好的性能。
14+
> 虽然有人说,使用 json 就是基本无视性能了。😂
15+
16+
### 内存占用
17+
一般来说,yaml 文件会比 json 文件略小一些,因为 yaml 使用换行做分隔符,而 json 中有大量的引号 `"` 和逗号 `,`, 所以 yaml 的内存效率会更高。
18+
19+
### 表现力
20+
除了绝对性能之外,还要考虑一些其他的因素,比如表现力或者说易读性,可理解性。
21+
毫无疑问,json 的语法更为简单,而 yaml 的语法复杂,还有各种极端情况和特殊场景,而且还有内部引用和注释,甚至 yaml 的语法还能兼容 json ,所以 yaml 的解释器也很复杂。
22+
23+
有人说 python 程序员更喜欢 yaml ,但是我不这么认为。
24+
25+
### 可移植性
26+
很难想象现在还有哪门现代编程语言不支持 json,一般起码都会有一个官方的标准库实现。而 yaml 并没有那么普及,一般最多也就是只有一个官方库的实现,而且还支持部分实现,没有完全实现 yaml 的全部语法。
27+
28+
## 简单使用
29+
30+
和 json 一样,主要就是序列化和反序列化操作,不过一般因为安全性问题,不直接使用 `load``dump` 而是使用 `safe_load``safe_dump`
31+
32+
```python
33+
# -*- coding: utf-8 -*-
34+
import yaml
35+
36+
37+
if __name__ == '__main__':
38+
data = {
39+
'name': 'ACME',
40+
'object': {'complex': True, 'description': 'complex object'},
41+
'shares': 100,
42+
'price': 542.23,
43+
'others': ["first thing", "second thing", "third thing"],
44+
}
45+
raw_yaml_data = yaml.safe_dump(data)
46+
print(raw_yaml_data)
47+
print(yaml.safe_load(raw_yaml_data))
48+
49+
```
50+
51+
执行结果
52+
53+
```
54+
$ python yaml_demo.py
55+
name: ACME
56+
object:
57+
complex: true
58+
description: complex object
59+
others:
60+
- first thing
61+
- second thing
62+
- third thing
63+
price: 542.23
64+
shares: 100
65+
66+
{'price': 542.23, 'object': {'complex': True, 'description': 'complex object'}, 'name': 'ACME', 'shares': 100, 'others': ['first thing', 'second thing', 'third thing']}
67+
```
68+
69+
使用 yaml 文件存取, 实际上还是同一个方法。
70+
71+
```python
72+
# -*- coding: utf-8 -*-
73+
import yaml
74+
75+
76+
if __name__ == '__main__':
77+
data = {
78+
'name': 'ACME',
79+
'object': {'complex': True, 'description': 'complex object'},
80+
'shares': 100,
81+
'price': 542.23,
82+
'others': ["first thing", "second thing", "third thing"],
83+
}
84+
with open('yaml_test.yaml', 'w') as f:
85+
raw_yaml_data = yaml.safe_dump(data, f)
86+
87+
with open('yaml_test.yaml', 'r') as f:
88+
print(yaml.safe_load(f))
89+
90+
```
91+
92+
## 参考链接
93+
94+
[13.10. json — JS 对象简谱](https://learnku.com/docs/pymotw/json-javascript-object-notation/3440)
95+
[YAML和JSON有什么区别?](https://my.oschina.net/u/3797416/blog/3147822)
96+
[YAML Ain’t Markup Language (YAMLTM) Version 1.2](https://yaml.org/spec/1.2/spec.html#id2759572)
97+
[YAML和JSON有什么区别?](https://www.codenong.com/1726802/)
98+
[What is the difference between YAML and JSON?](https://stackoverflow.com/questions/1726802/what-is-the-difference-between-yaml-and-json)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /