PyYAML使用
install
yum -y install PyYAML
document
http://www.showyounger.com/show/101586.html
http://pyyaml.org/wiki/PyYAMLDocumentation
yaml.load() 将yaml文档处理为python对象
yaml.dump() 将python对象处理为yaml文档
example
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import yaml
with file('/tmp/dashboard.yaml', 'r') as f:
content = yaml.load(f)
print content['web']['login'], content['web']['password'], content['interval']
for switch in content['switch']:
print switch['name'], switch['ip'], switch['snmp'], switch['port']
new = {'name': 'bj', 'ip': '192.168.1.100', 'community': 'passwd', 'port': 'GigabitEthernet 0/48', 'bandwidth': 10000}
content['switch'].append(new)
with file('/tmp/dashboard.yaml', 'w') as f:
f.write(yaml.dump(content, indent=4, default_flow_style=False))
# yaml.dump(content, f)
flow_style
interval: 60
switch:
- {ip: 192.168.3.100, name: gz, port: Port-channel 1, community: passwd, bandwidth: 300}
- {ip: 192.168.2.100, name: sh, port: Port-channel 4, community: passwd, bandwidth: 1000}
- {ip: 192.168.1.100, name: bj, port: GigabitEthernet 0/48, community: passwd, bandwidth: 10000}
web: {login: admin, password: 123456}
interval: 60
switch:
- ip: 192.168.3.100
name: gz
port: Port-channel 1
community: passwd
bandwidth: 300
- ip: 192.168.2.100
name: sh
port: Port-channel 4
community: passwd
bandwidth: 1000
- ip: 192.168.1.100
name: bj
port: GigabitEthernet 0/48
community: passwd
bandwidth: 10000
web:
login: admin
password: 123456
PyYAML使用的更多相关文章
- Python自动化运维之10、模块之json、pickle、XML、PyYAML、configparser、shutil
序列化 Python中用于序列化的两个模块 json 用于[字符串]和 [python基本数据类型] 间进行转换 pickle 用于[python特有的类型] 和 [python基本数据类 ...
- Python学习笔记——基础篇【第六周】——PyYAML & configparser模块
PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之Co ...
- Python 3.6.4 / win10 使用pip安装keras时遇到依赖的PyYAML安装出错
PS C:\Users\myjac\Desktop\simple-chinese-ocr> pip install keras Collecting keras Downloading http ...
- Python【pyyaml】模块
pyyaml模块安装: pip install pyyaml pyyaml导入: import yaml pyyaml使用: 1.使用前,在pycharm中新建一个以yaml或yml结尾的文件,保存为 ...
- Pip无法卸载某些包:Cannot uninstall 'PyYAML'.
查找了很多资料,最终还是手动删除吧: 注意如果你有火萤酱或everything等外部索引的,来搜索如图PyYAML的进行删除,可能删不干净 建议最后在你的anaconda路径下或者python路径下在 ...
- Python3学习之路~5.10 PyYAML模块
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation
- Python pyYAML模块
pyYaml模块 一.pyYAML python也可以很容的处理ymal文档格式,只不过取药安装一个模块. 语法例子 注:主要用来做配置文件. 注:需要下载标准库.
- 学习pyyaml
网上查了一圈,觉得较好的yaml教程有: YAML 语言教程 :http://www.ruanyifeng.com/blog/2016/07/yaml.html. 另外,在github的pyyaml库 ...
- python基础-PyYaml操作yaml文件
yaml语法 格式 它的基本语法规则如下 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 YAML 支持的数据结构有 ...
- pip安装kolla-ansible时报错Cannot install 'PyYAML'的解决方法
pip install kolla-ansible --ignore-installed PyYAML
随机推荐
- 使用instsrv.exe和srvany.exe将应用程序安装成windows后台服务
好的参考链接: http://www.jb51.net/softjc/374631.html 利用这两个工具,将 exe程序或者 bat文件,做成Windows后台服务.
- Linq,拉姆达表达式注意!
linq的条件语句尽量不要出现计算的式子,要不然很可能,程序不能正确的将这些复杂的式子编译成表达式!! 例如: dataContext.Assets.Count(s => s.SubCatego ...
- Unix shell判断和比较
1. shell 的$! ,$?, $$,$@ $n $1 the first parameter,$2 the second... $# The number of c ...
- Redis未授权访问攻击过程与防范
一.Redis未授权访问攻击过程 攻击主机:kali 目标主机:centos6.8(10.104.11.178) Redis版本:2.8 攻击条件:默认配置,未进行认证 攻击步骤详解: 1.Kali攻 ...
- Eclipse + Git + 码云
1. 进入码云个人首页 点击自己的名称即可 2. 添加一个项目 3. 在Eclipse中创建一个本地Git Eclipse不建议git目录创建在项目的目录下,因此另选一个目录作为本地Git目录 选择一 ...
- team foundation server 工具的使用
1.打开TFS工具点击创建集合,填写集合名称,下一步,按照步骤一步一步的往下操作. 2.创建完集合以后,打开VS工具,如下图,在主页上左键选择新建团队项目. 3.选择团队项目,然后选择新建的项目集合和 ...
- TCP/IP协议<二>
一.TCP/IP的标准化 1.TCP/IP的含义 一般来说,TCP/IP是利用IP进行通信时所必须用到的协议群的统称. 具体点,IP或ICMP.TCP或UDP.TELENT或FTP.以及HTTP等都属 ...
- python Gensim库建立word2vec参数说明
from gensim.models import word2vec model = word2vec.Word2Vec(sentences, size=80, window=10,workers=6 ...
- Searching the Web论文阅读
Searching the Web (Arvind Arasu etc.) 1. 概述 2000年,23%网页每天更新,.com域内网页40%每天更新.网页生存半衰期是10天.描述方法可用Pois ...
- KEIL中头文件使用配置向导
在xxx.h头文件的代码中夹杂的“<h> </h>”.“<o>”“<i>”.“<c1> </c>”和“<e& ...