在Python使用yaml的几个例子

简介:

python版本:2.7.5

安装方法:pip install PyYaml


“把变量写进yaml做配置文件,然后python脚本从yaml文件里面取到变量”的方法最近是在python编程里比较流行的配置项方法。yaml更加易读,而且通过缩进表示结构,这一点与python不谋而合。


Yaml有四个比较常用的用法,分别是load()、dump()、load_all()、dump_all()。这篇文章主要就是了解一下这四个方法。


首先我们先写一个很简单的test.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import  yaml
 
yaml_str =  "" "
name: Gakki
age: 29
job: Actress
relationship: Wife
"" "
 
aaa = yaml.load(yaml_str)
print aaa

执行的话,看到的效果就是:

1
2
[root@paas-online-crs-001 chentest] # python test.py 
{ 'job' 'Actress' 'age' : 29,  'relationship' 'Wife' 'name' 'Gakki' }


这个aaa的类型是一个字典(dict),如果要得到里面那个"Gakki",那么就是aaa['name']。通过load方法,一个字符串变成了一个字典。


现在把test.py换成如下:

1
2
3
4
5
6
7
8
9
10
11
12
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import  yaml
 
yaml_dict = { "name" "Gakki" ,
          "age" : 29,
          "job" "Actress" ,
          "relationship" "Wife"
               }
aaa = yaml.dump(yaml_dict, default_flow_style=False)
print aaa
print ( type (aaa))

执行后的效果如下:

1
2
3
4
5
6
[root@paas - online - crs - 001  chentest] # python test.py 
age:  29
job: Actress
name: Gakki
relationship: Wife
< type  'str' >


可见,通过dump方法,把一个dict变成了一个字符串。


现在写一个配置文件,假如它叫test.yaml:

1
2
3
4
- Gakki
- 29
- Actress
- Wife


再来一个test.py,内容如下: 

1
2
3
4
5
6
7
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import  yaml
 
aaa  =  yaml.load( file ( 'test.yaml' 'r' ))
print  aaa
print  ( type (aaa))

执行这个test.py:

1
2
3
[root@paas-online-crs-001 chentest] # python test.py 
[ 'Gakki' , 29,  'Actress' 'Wife' ]
< type  'list' >     #得到了一个列表


如果把那个test.yaml升级成字典和列表的混合结构,如下:

1
2
3
4
5
6
7
- name: Chris
   age: 29
   job: OM Engineer
- name: Gakki
   age: 29
   job: Actress
   relationship: Wife


执行test.py的效果如下:

1
2
3
[root@paas - online - crs - 001  chentest] # python test.py 
[{ 'job' 'OM Engineer' 'age' 29 'name' 'Chris' }, { 'job' 'Actress' 'age' 29 'relationship' 'Wife' 'name' 'Gakki' }]
< type  'list' >

既然获得的结果是一个包含字典的列表,那么如果要获得“Gakki”就是aaa[1]['name']


如果想要复制和引用,那么要用&和*,比如把test.yaml改成这样:

1
2
name: &name Gakki
wife: *name

执行test.py的效果如下:

1
2
3
[root@paas-online-crs-001 chentest] # python test.py 
{ 'name' 'Gakki' 'wife' 'Gakki' }
< type  'dict' >


在同一个yaml文件中,可以用 --- 来分段,这样可以将多个文档写在一个文件中:

1
2
3
4
5
6
7
8
9
- - -
   name: Chris
   age:  29
   job: OM Engineer
- - -
   name: Gakki
   age:  29
   job: Actress
   relationship: Wife

在写一个新的test.py如下: 

1
2
3
4
5
6
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import yaml
ys = yaml.load_all(file( 'gakki.yaml' 'r' ))     #load_all() 方法会生成一个迭代器,可以用for输出出来
for  in  ys:
     print y

执行这个py的效果:

1
2
3
[root@paas-online-crs-001 chentest] # python test.py 
{ 'job' 'OM Engineer' 'age' : 29,  'name' 'Chris' }
{ 'job' 'Actress' 'age' : 29,  'relationship' 'Wife' 'name' 'Gakki' }


参考文档:https://huilansame.github.io/huilansame.github.io/archivers/recommond-case-file-type-yaml


 本文转自 苏幕遮618 51CTO博客,原文链接:http://blog.51cto.com/chenx1242/2046103


相关文章
|
4月前
|
存储 JSON JavaScript
【chat-gpt问答记录】python将数据存为json格式和yaml格式
【chat-gpt问答记录】python将数据存为json格式和yaml格式
72 1
|
27天前
|
Python
Python 解析 yaml 配置文件
Python 解析 yaml 配置文件
32 0
|
1月前
|
Python
7-9|Python如何安装ruamel.yaml
7-9|Python如何安装ruamel.yaml
|
2月前
|
机器学习/深度学习 计算机视觉 Python
深度学习项目中在yaml文件中定义配置,以及使用的python的PyYAML库包读取解析yaml配置文件
深度学习项目中在yaml文件中定义配置,以及使用的python的PyYAML库包读取解析yaml配置文件
63 0
|
3月前
|
Python
python存取yaml文件
python存取yaml文件
|
4月前
|
数据安全/隐私保护 Python
经验大分享:python读取yaml文件
经验大分享:python读取yaml文件
119 0
|
XML JSON 数据格式
python yaml操作
python yaml操作
108 0
|
机器学习/深度学习 数据挖掘 数据处理
5个例子比较Python Pandas 和R data.table
5个例子比较Python Pandas 和R data.table
134 0
5个例子比较Python Pandas 和R data.table
|
索引 Python
Python基础教程(第二版)例子程序__getitem__函数
Python基础教程(第二版)例子程序__getitem__函数
128 2
|
XML JSON 数据格式
python小知识-好用的配置工具yaml
配置是开发中常用的手段,通过配置可以灵活的设置可变的参数,而不需要调整代码。 python中有使用不同的文件格式来做配置,如json,xml等。今天这里分享一个好用的配置工具yaml文件。
379 0