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
随机推荐
- sharepoint_study_12
描述:SharePoint新建Web应用程序时提示如下错误: 解决: 1. Go to IIS 2. Select the DefaultAppPool and Go to the Advanced ...
- Luogu P2107 小Z的AK计划 堆贪心
好久不做这种题了... 存一下每个点的位置和时间,由于达到某个位置跟之前去哪里AK的无关,所以在时间超限后,可以用大根堆弹掉之前消耗时间最大的,来更新答案,相当于去掉之前花费最大的,直到时间不在超限. ...
- vue.js学习笔记(二)——vue-router详解
vue-router详解 原文链接:www.jianshu.com 一.前言 要学习vue-router就要先知道这里的路由是什么?为什么我们不能像原来一样直接用<a></a> ...
- Go语言基础之6--值类型和引用类型
一. 引用类型 引用类型理解为(C语言):指针 Golang中只有三种引用类型:slice(切片).map(字典).channel(管道): 实例1-1 package main import &qu ...
- Hibernate JPA 如何使用SQL文直接查询
对于复杂SQL,可以直接调用native sql来是实现 public List<Partner> findPartners() { List<Partner> partner ...
- STL之set(唯一且有顺序)
set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据, 在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序.应该注意的是set中数元素的值不能直接被改变. ...
- 【ACM】懒省事的小明
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...
- vue-基于elementui自定义主题更换皮肤及自定义内容的皮肤跟换
参考这篇博客https://blog.csdn.net/young_Emily/article/details/78591261做一遍,加上自己的一些理解 思路:通过自己上一篇博客https://ww ...
- spring AOP注解
此段小代码演示了spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑.@After 是在所拦截方法执行之后执行一段逻辑.@A ...
- 持久层框架---jdbc
1.JDBC编程步骤: 1.1 加载数据库驱动: 1.2 获取数据库连接: 1.3 通过Connection对象创建Statement对象: 1.4 使用Statement对象执行SQL语句: 1.5 ...