YAML 语言和 Python 中的使用


Author: yifei / Created: May 29, 2017, 11:20 p.m. / Modified: May 29, 2017, 11:21 p.m. / Edit

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)


有任何问题可以发邮件到 kongyifei (at) gmail.com 讨论