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
随机推荐
- FMDB----SQL----数据库
#pragma mark -- 数库 - (void)createDatabase{ //路径 NSString *path = [NSString stringWithFormat:@"% ...
- shell脚本安装jdk
#!/bin/bash BASE_SERVER=192.168.1.11 yum install -y wget wget $BASE_SERVER/soft-all/jdk-7u45-linux-x ...
- JScrollPane (滚动面板)使用心得
注意:使用滚动面板时,必须指定内部组件是哪个组件 JScrollPane的两种使用方式:. 方式一: //直接在创建滚动面板对象时,就指定所要显示的组件 //本例中所要显示的是jPanel JPane ...
- C语言的存储类别和动态内存分配
存储类别分三大类: 静态存储类别 自动存储类别 动态分配内存 变量.对象--->内存管理 内存考虑效率(时间更短.空间更小) 作用域 链接.---->空间 存储器 ----->时 ...
- AngularJs 中使用OpenLayer例子,手机端地图加载不显示问题
var map, toolip, overlay; var layer = new ol.layer.Vector(); var imgLayer = ...
- 神奇的操作--O(1)快速乘
从同机房大佬那里听来的... 用O(1)时间求出两个相乘超过long long的数的取摸的结果 神奇的操作... inline long long multi(long long x,long lon ...
- UESTC 1437
LCA模板题 随便找的倍增模板... #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+11; int t ...
- 实时同步inotify+rsync
目的,要求 nfs储存服务器与backup备份服务器,数据同步,万一nfs储存服务器挂了,数据还在 实时同步备份软件服务 1)inotify 实时同步软件 2)sersync 实时同步软件 实时同步原 ...
- vue 组件之间数据传递(七)
1.props:父组件 -->传值到子组件 app.vue是父组件 ,其它组件是子组件,把父组件值传递给子组件需要使用 =>props 在父组件(App.vue)定义一个属性(变量)sex ...
- python_案例综合:教材记录管理
class Book(): def __init__(self,ISBN,Ftile,Author,Publisher): self.ISBN = ISBN self.Ftile = Ftile se ...