python 使用yaml模块
<p>python:yaml模块<br>一、yaml文件介绍<br>YAML是一种简洁的非标记语言。其以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁。<br>1. yaml文件规则<br>基本规则:<br> 大小写敏感<br> 使用缩进表示层级关系<br> 缩进时不允许使用Tab键,只允许使用空格。<br> 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可<br> 使用#表示注释<br> 字符串可以不用引号标注<br><br>2. yaml文件数据结构<br><br> 对象:键值对的集合(简称 "映射或字典")<br> 键值对用冒号 “:” 结构表示,冒号与值之间需用空格分隔<br> 数组:一组按序排列的值(简称 "序列或列表")<br> 数组前加有 “-” 符号,符号与值之间需用空格分隔<br> 纯量(scalars):单个的、不可再分的值(如:字符串、bool值、整数、浮点数、时间、日期、null等)<br> None值可用null可 ~ 表示</p><p>二、安装yaml<br><br>pip命令: pip install PyYaml<br>引入:import yaml<br>用python读取yaml文件如下:</p>
<p>代码:<br>import yaml<br>from Common.dir_config import *<br><br># 打开yaml文件<br>fs = open(os.path.join(caps_dir, "data.yaml"),encoding="UTF-8")<br>datas = yaml.load(fs)<br>print(datas)<br>备注:yaml版本5.1之后弃用,YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated<br>代码改后:<br>import yaml<br>from Common.dir_config import *<br><br># 打开yaml文件<br>fs = open(os.path.join(caps_dir, "data.yaml"),encoding="UTF-8")<br>datas = yaml.load(fs,Loader=yaml.FullLoader) #添加后就不警告了<br>print(datas)</p>
<p><br>三、python中读取yaml配置文件<br>1. 前提条件<br><br>python中读取yaml文件前需要安装pyyaml和导入yaml模块:<br><br> 使用yaml需要安装的模块为pyyaml(pip3 install pyyaml);<br> 导入的模块为yaml(import yaml)<br><br>2. 读取yaml文件数据<br><br>python通过open方式读取文件数据,再通过load函数将数据转化为列表或字典;<br><br>import yaml<br>import os<br><br>def get_yaml_data(yaml_file):<br> # 打开yaml文件<br> print("***获取yaml文件数据***")<br> file = open(yaml_file, 'r', encoding="utf-8")<br> file_data = file.read()<br> file.close()<br> <br> print(file_data)<br> print("类型:", type(file_data))<br><br> # 将字符串转化为字典或列表<br> print("***转化yaml数据为字典或列表***")<br> data = yaml.load(file_data)<br> print(data)<br> print("类型:", type(data))<br> return data<br>current_path = os.path.abspath(".")<br>yaml_path = os.path.join(current_path, "config.yaml")<br>get_yaml_data(yaml_path)<br><br>"""<br>***获取yaml文件数据***<br># yaml键值对:即python中字典<br>usr: my<br>psw: 123455<br>类型:<class 'str'><br>***转化yaml数据为字典或列表***<br>{'usr': 'my', 'psw': 123455}<br>类型:<class 'dict'><br>"""<br><br>3. yaml文件数据为键值对<br><br>(1)yaml文件中内容为键值对:<br><br># yaml键值对:即python中字典<br>usr: my<br>psw: 123455<br>s: " abc\n"<br><br>python解析yaml文件后获取的数据:<br><br>{'usr': 'my', 'psw': 123455, 's': ' abc\n'}<br><br>(2)yaml文件中内容为“键值对'嵌套"键值对"<br><br># yaml键值对嵌套:即python中字典嵌套字典<br>usr1:<br> name: a<br> psw: 123<br>usr2:<br> name: b<br> psw: 456<br><br>python解析yaml文件后获取的数据:<br><br>{'usr1': {'name': 'a', 'psw': 123}, 'usr2': {'name': 'b', 'psw': 456}}<br><br>(3)yaml文件中“键值对”中嵌套“数组”<br><br># yaml键值对中嵌套数组<br>usr3:<br> - a<br> - b<br> - c<br>usr4:<br> - b<br><br>python解析yaml文件后获取的数据:<br><br>{'usr3': ['a', 'b', 'c'], 'usr4': ['b']}<br><br>4. yaml文件数据为数组<br><br>(1)yaml文件中内容为数组<br><br># yaml数组<br>- a<br>- b<br>- 5<br><br>python解析yaml文件后获取的数据:<br><br>['a', 'b', 5]<br><br>(2)yaml文件“数组”中嵌套“键值对”<br><br># yaml"数组"中嵌套"键值对"<br>- usr1: aaa<br>- psw1: 111<br> usr2: bbb<br> psw2: 222<br><br>python解析yaml文件后获取的数据:<br><br>[{'usr1': 'aaa'}, {'psw1': 111, 'usr2': 'bbb', 'psw2': 222}]<br><br>5. yaml文件中基本数据类型:<br><br># 纯量<br>s_val: name # 字符串:{'s_val': 'name'}<br>spec_s_val: "name\n" # 特殊字符串:{'spec_s_val': 'name\n'<br>num_val: 31.14 # 数字:{'num_val': 31.14}<br>bol_val: true # 布尔值:{'bol_val': True}<br>nul_val: null # null值:{'nul_val': None}<br>nul_val1: ~ # null值:{'nul_val1': None}<br>time_val: 2018-03-01t11:33:22.55-06:00 # 时间值:{'time_val': datetime.datetime(2018, 3, 1, 17, 33, 22, 550000)}<br>date_val: 2019-01-10 # 日期值:{'date_val': datetime.date(2019, 1, 10)}<br><br>6. yaml文件中引用<br><br>yaml文件中内容<br><br>animal3: &animal3 fish<br>test: *animal3<br><br>python读取的数据<br><br>{'animal3': 'fish', 'test': 'fish'}<br><br>三、python中读取多个yaml文档<br>1. 多个文档在一个yaml文件,使用 --- 分隔方式来分段<br><br>如:yaml文件中数据<br><br># 分段yaml文件中多个文档<br>---<br>animal1: dog<br>age: 2<br>---<br>animal2: cat<br>age: 3<br><br>2. python脚本读取一个yaml文件中多个文档方法<br><br>python获取yaml数据时需使用load_all函数来解析全部的文档,再从中读取对象中的数据<br><br># yaml文件中含有多个文档时,分别获取文档中数据<br>def get_yaml_load_all(yaml_file):<br> # 打开yaml文件<br> file = open(yaml_file, 'r', encoding="utf-8")<br> file_data = file.read()<br> file.close()<br> all_data = yaml.load_all(file_data)<br> for data in all_data:<br> print(data)<br>current_path = os.path.abspath(".")<br>yaml_path = os.path.join(current_path, "config.yaml")<br>get_yaml_load_all(yaml_path)<br>"""结果<br>{'animal1': 'dog', 'age': 2}<br>{'animal2': 'cat', 'age': 3}<br>"""<br><br>四、python对象生成yaml文档<br>1. 直接导入yaml(即import yaml)生成的yaml文档<br><br>通过yaml.dump()方法不会将列表或字典数据进行转化yaml标准模式,只会将数据生成到yaml文档中<br><br># 将python对象生成yaml文档<br>import yaml<br>def generate_yaml_doc(yaml_file):<br> py_object = {'school': 'zhang',<br> 'students': ['a', 'b']}<br> file = open(yaml_file, 'w', encoding='utf-8')<br> yaml.dump(py_object, file)<br> file.close()<br>current_path = os.path.abspath(".")<br>yaml_path = os.path.join(current_path, "generate.yaml")<br>generate_yaml_doc(yaml_path)<br>"""结果<br>school: zhang<br>students: <br>"""<br><br>2. 使用ruamel模块中的yaml方法生成标准的yaml文档<br><br>(1)使用ruamel模块中yaml前提条件<br><br> 使用yaml需要安装的模块:ruamel.yaml(pip3 install ruamel.yaml);<br> 导入的模块:from ruamel import yaml<br><br>(2)ruamel模块生成yaml文档<br><br>def generate_yaml_doc_ruamel(yaml_file):<br> from ruamel import yaml<br> py_object = {'school': 'zhang',<br> 'students': ['a', 'b']}<br> file = open(yaml_file, 'w', encoding='utf-8')<br> yaml.dump(py_object, file, Dumper=yaml.RoundTripDumper)<br> file.close()<br>current_path = os.path.abspath(".")<br>yaml_path = os.path.join(current_path, "generate.yaml")<br>generate_yaml_doc_ruamel(yaml_path)<br>"""结果<br>school: zhang<br>students:<br>- a<br>- b<br>"""<br>(3)ruamel模块读取yaml文档<br><br># 通过from ruamel import yaml读取yaml文件<br>def get_yaml_data_ruamel(yaml_file):<br> from ruamel import yaml<br> file = open(yaml_file, 'r', encoding='utf-8')<br> data = yaml.load(file.read(), Loader=yaml.Loader)<br> file.close()<br> print(data)<br>current_path = os.path.abspath(".")<br>yaml_path = os.path.join(current_path, "dict_config.yaml")<br>get_yaml_data_ruamel(yaml_path)</p>
<p>**************************************************</p>
<p>上代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> yaml
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> os
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">单个文档</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> get_yaml_data(yaml_file):
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">打开yaml文件</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">***获取yam文件数据***</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">10</span> file=open(yaml_file,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">r</span><span style="color: rgba(128, 0, 0, 1)">'</span>,encoding=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">11</span> file_data=<span style="color: rgba(0, 0, 0, 1)">file.read()
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)"> file.close()
</span><span style="color: rgba(0, 128, 128, 1)">13</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(file_data)
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">类型</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,type(file_data))
</span><span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">将字符串转化为字典或列表</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">***转化yaml数据为字典或列表***</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">19</span> data=yaml.safe_load(file_data)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">safe_load,safe_load,unsafe_load</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(data)
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">类型</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,type(data))
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> data
</span><span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> current_path=os.path.abspath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">25</span> yaml_path=os.path.join(current_path,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">config.yaml</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--------------------</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,yaml_path)
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 0, 0, 1)">get_yaml_data(yaml_path)
</span><span style="color: rgba(0, 128, 128, 1)">28</span>
<span style="color: rgba(0, 128, 128, 1)">29</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">yaml文件中含多个文档时,分别获取文档中数据</span>
<span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> get_yaml_load_all(yaml_file):
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">打开文件</span>
<span style="color: rgba(0, 128, 128, 1)">33</span> file=open(yaml_file,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">r</span><span style="color: rgba(128, 0, 0, 1)">'</span>,encoding=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">34</span> file_data=<span style="color: rgba(0, 0, 0, 1)">file.read()
</span><span style="color: rgba(0, 128, 128, 1)">35</span> <span style="color: rgba(0, 0, 0, 1)"> file.close()
</span><span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span> all_data=yaml.load_all(file_data,Loader=<span style="color: rgba(0, 0, 0, 1)">yaml.FullLoader)
</span><span style="color: rgba(0, 128, 128, 1)">38</span> <span style="color: rgba(0, 0, 255, 1)">for</span> data <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> all_data:
</span><span style="color: rgba(0, 128, 128, 1)">39</span> <span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">data-----</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,data)
</span><span style="color: rgba(0, 128, 128, 1)">40</span>
<span style="color: rgba(0, 128, 128, 1)">41</span> current_path=os.path.abspath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">42</span> yaml_path=os.path.join(current_path,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">configall.yaml</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)">get_yaml_load_all(yaml_path)
</span><span style="color: rgba(0, 128, 128, 1)">44</span>
<span style="color: rgba(0, 128, 128, 1)">45</span>
<span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">生成yaml文档</span>
<span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> generate_yaml_doc(yaml_file):
</span><span style="color: rgba(0, 128, 128, 1)">48</span> py_ob={<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">school</span><span style="color: rgba(128, 0, 0, 1)">"</span>:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">zhang</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">students</span><span style="color: rgba(128, 0, 0, 1)">"</span>:[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">b</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">]}
</span><span style="color: rgba(0, 128, 128, 1)">50</span> file=open(yaml_file,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">w</span><span style="color: rgba(128, 0, 0, 1)">'</span>,encoding=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">51</span> <span style="color: rgba(0, 0, 0, 1)"> yaml.dump(py_ob,file)
</span><span style="color: rgba(0, 128, 128, 1)">52</span> <span style="color: rgba(0, 0, 0, 1)"> file.close()
</span><span style="color: rgba(0, 128, 128, 1)">53</span>
<span style="color: rgba(0, 128, 128, 1)">54</span> current_path=os.path.abspath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">55</span> yaml_path=os.path.join(current_path,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">generate.yaml</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">56</span> generate_yaml_doc(yaml_path)</pre>
</div>
<p>执行结果</p>
<p><img src="https://img2018.cnblogs.com/blog/956249/201910/956249-20191030155528740-912201978.png"></p>
<p> </p>
<p> </p>
<p><br>原文:https://www.jianshu.com/p/eaa1bf01b3a6<br><br></p><br><br>
来源:https://www.cnblogs.com/lisa2016/p/11764808.html
頁:
[1]