YAML 语言和 Python 中的使用

Install

pip install pyyaml

Basic Example

import yaml
result = yaml.load(yaml_string) # to python list or dict
string = yaml.dump(py_object)
# NOTE: unlike the json module, the method here is load/dump, not loads/dumps

Yaml

YAML uses three dashes (“—”) to separate documents within a stream. Three dots ( “…”) indicate the end of a document without starting a new one, for use in communication channels.

empty string in yaml is ” or “”, if you have a blank entry, it will be converted to None
you don’t need to quote strings in yaml

for dumping, when dumping to a file, do this

with open('file.yml', 'w') as f:
    yaml.dump(data, f, default_flow_style=False, allow_unicode=True)

yaml 中还可以通过 &

Reference

  1. https://stackoverflow.com/questions/45805380/meaning-of-ampersand-in-docker-compose-yml-file

及时获取更新,请关注公众号“爬虫技术学习”(spider-learn)

多年大厂求职&面试官经验,简历付费优化,¥ 500/次。

公众号“爬虫技术学习(spider-learn)”

About 逸飞

后端工程师

发表评论

邮箱地址不会被公开。 必填项已用*标注