【Python自动化运维之路Day7】
1. configparser模块
import configparser
config = configparser.ConfigParser() #先把config应用一下configparser,个人感觉有点像logging模块中的logger一样
config.read('db',encoding='utf-8') #读取配置文件,编码类型为utf-8
#使用sections()方法读取所有section,以列表形式返回
result = config.sections()
print(result)
#获取指定section下所有键值对
result = config.items('section1')
print(result)
#获取指定节点下所有的键
result = config.options('section1')
print(result)
#获取指定节点下键的值
result = config.get('section1','k2')
print(result)
####检查、删除、添加特定section######
#检查指定的section是否存在,返回一个布尔值,存在为True
result = config.has_section('mysqld')
print(result)
#结果: True
#添加节点 mysqldump
config.add_section('mysqldump')
config.write(open('db','w')) #需要使用write方法写入内存数据到配置文件中,不然是不能持久化到文件的
result = config.sections()
print(result)
#删除节点,mysqldump
config.remove_section('mysqldump') #使用remove()方法
config.write(open('my.cnf','w')) #同样默认是在内存中操作,需要调用write方法,将内存数据写入到文件来持久化存储
result = config.sections()
print(result) #########检查、删除设置section内的key-value########
#使用has_option方法,返回一个布尔值,存在为True
result = config.has_option('mysqldump','socket')
print(result)
#在mysqld中添加 键 innodb_file_per_table 值为1
#使用set方法
config.set('mysqldump','innodb_file_per_table','')
config.write(open('my.cnf','w')) #同样需要写入内存数据到文件,使用write方法
result = config.options('mysqld')
print(result)
#删除mysqld下的socket键
#使用remove_option()方法
config.remove_option('mysqldump','innodb_file_per_table')
config.write(open('my.cnf','w')) #写入内存数据到文件
result = config.options('mysqld')
print(result)
2 xml模块
1、打开文件的俩种方式
from xml.etree import ElementTree as ET #打开文件的俩种方式
result_xml = open('page.xml','r').read()
#将字符串解析成xml特殊对象,root指的是xml文件的根节点
root = ET.XML(result_xml)
print(root)
#使用xml.etree.ElementTree.parse()函数解析整个xml文件并将其转换成一个文档对象。
tree = ET.parse('example.xml')
root = tree.getroot()
print(root)
2、节点功能
xml格式类型是节点里嵌套节点 ,所以对于每一个节点都有如下的功能:
class Element:
"""An XML element. This class is the reference implementation of the Element interface. An element's length is its number of subelements. That means if you
want to check if an element is truly empty, you should check BOTH
its length AND its text attribute. The element tag, attribute names, and attribute values can be either
bytes or strings. *tag* is the element name. *attrib* is an optional dictionary containing
element attributes. *extra* are additional element attributes given as
keyword arguments. Example form:
<tag attrib>text<child/>...</tag>tail """ 当前节点的标签名
tag = None
"""The element's name.""" 当前节点的属性 attrib = None
"""Dictionary of the element's attributes.""" 当前节点的内容
text = None
"""
Text before first subelement. This is either a string or the value None.
Note that if there is no text, this attribute may be either
None or the empty string, depending on the parser. """ tail = None
"""
Text after this element's end tag, but before the next sibling element's
start tag. This is either a string or the value None. Note that if there
was no text, this attribute may be either None or an empty string,
depending on the parser. """ def __init__(self, tag, attrib={}, **extra):
if not isinstance(attrib, dict):
raise TypeError("attrib must be dict, not %s" % (
attrib.__class__.__name__,))
attrib = attrib.copy()
attrib.update(extra)
self.tag = tag
self.attrib = attrib
self._children = [] def __repr__(self):
return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self)) def makeelement(self, tag, attrib):
创建一个新节点
"""Create a new element with the same type. *tag* is a string containing the element name.
*attrib* is a dictionary containing the element attributes. Do not call this method, use the SubElement factory function instead. """
return self.__class__(tag, attrib) def copy(self):
"""Return copy of current element. This creates a shallow copy. Subelements will be shared with the
original tree. """
elem = self.makeelement(self.tag, self.attrib)
elem.text = self.text
elem.tail = self.tail
elem[:] = self
return elem def __len__(self):
return len(self._children) def __bool__(self):
warnings.warn(
"The behavior of this method will change in future versions. "
"Use specific 'len(elem)' or 'elem is not None' test instead.",
FutureWarning, stacklevel=2
)
return len(self._children) != 0 # emulate old behaviour, for now def __getitem__(self, index):
return self._children[index] def __setitem__(self, index, element):
# if isinstance(index, slice):
# for elt in element:
# assert iselement(elt)
# else:
# assert iselement(element)
self._children[index] = element def __delitem__(self, index):
del self._children[index] def append(self, subelement):
为当前节点追加一个子节点
"""Add *subelement* to the end of this element. The new element will appear in document order after the last existing
subelement (or directly after the text, if it's the first subelement),
but before the end tag for this element. """
self._assert_is_element(subelement)
self._children.append(subelement) def extend(self, elements):
为当前节点扩展 n 个子节点
"""Append subelements from a sequence. *elements* is a sequence with zero or more elements. """
for element in elements:
self._assert_is_element(element)
self._children.extend(elements) def insert(self, index, subelement):
在当前节点的子节点中插入某个节点,即:为当前节点创建子节点,然后插入指定位置
"""Insert *subelement* at position *index*."""
self._assert_is_element(subelement)
self._children.insert(index, subelement) def _assert_is_element(self, e):
# Need to refer to the actual Python implementation, not the
# shadowing C implementation.
if not isinstance(e, _Element_Py):
raise TypeError('expected an Element, not %s' % type(e).__name__) def remove(self, subelement):
在当前节点在子节点中删除某个节点
"""Remove matching subelement. Unlike the find methods, this method compares elements based on
identity, NOT ON tag value or contents. To remove subelements by
other means, the easiest way is to use a list comprehension to
select what elements to keep, and then use slice assignment to update
the parent element. ValueError is raised if a matching element could not be found. """
# assert iselement(element)
self._children.remove(subelement) def getchildren(self):
获取所有的子节点(废弃)
"""(Deprecated) Return all subelements. Elements are returned in document order. """
warnings.warn(
"This method will be removed in future versions. "
"Use 'list(elem)' or iteration over elem instead.",
DeprecationWarning, stacklevel=2
)
return self._children def find(self, path, namespaces=None):
获取第一个寻找到的子节点
"""Find first matching element by tag name or path. *path* is a string having either an element tag or an XPath,
*namespaces* is an optional mapping from namespace prefix to full name. Return the first matching element, or None if no element was found. """
return ElementPath.find(self, path, namespaces) def findtext(self, path, default=None, namespaces=None):
获取第一个寻找到的子节点的内容
"""Find text for first matching element by tag name or path. *path* is a string having either an element tag or an XPath,
*default* is the value to return if the element was not found,
*namespaces* is an optional mapping from namespace prefix to full name. Return text content of first matching element, or default value if
none was found. Note that if an element is found having no text
content, the empty string is returned. """
return ElementPath.findtext(self, path, default, namespaces) def findall(self, path, namespaces=None):
获取所有的子节点
"""Find all matching subelements by tag name or path. *path* is a string having either an element tag or an XPath,
*namespaces* is an optional mapping from namespace prefix to full name. Returns list containing all matching elements in document order. """
return ElementPath.findall(self, path, namespaces) def iterfind(self, path, namespaces=None):
获取所有指定的节点,并创建一个迭代器(可以被for循环)
"""Find all matching subelements by tag name or path. *path* is a string having either an element tag or an XPath,
*namespaces* is an optional mapping from namespace prefix to full name. Return an iterable yielding all matching elements in document order. """
return ElementPath.iterfind(self, path, namespaces) def clear(self):
清空节点
"""Reset element. This function removes all subelements, clears all attributes, and sets
the text and tail attributes to None. """
self.attrib.clear()
self._children = []
self.text = self.tail = None def get(self, key, default=None):
获取当前节点的属性值
"""Get element attribute. Equivalent to attrib.get, but some implementations may handle this a
bit more efficiently. *key* is what attribute to look for, and
*default* is what to return if the attribute was not found. Returns a string containing the attribute value, or the default if
attribute was not found. """
return self.attrib.get(key, default) def set(self, key, value):
为当前节点设置属性值
"""Set element attribute. Equivalent to attrib[key] = value, but some implementations may handle
this a bit more efficiently. *key* is what attribute to set, and
*value* is the attribute value to set it to. """
self.attrib[key] = value def keys(self):
获取当前节点的所有属性的 key """Get list of attribute names. Names are returned in an arbitrary order, just like an ordinary
Python dict. Equivalent to attrib.keys() """
return self.attrib.keys() def items(self):
获取当前节点的所有属性值,每个属性都是一个键值对
"""Get element attributes as a sequence. The attributes are returned in arbitrary order. Equivalent to
attrib.items(). Return a list of (name, value) tuples. """
return self.attrib.items() def iter(self, tag=None):
在当前节点的子孙中根据节点名称寻找所有指定的节点,并返回一个迭代器(可以被for循环)。
"""Create tree iterator. The iterator loops over the element and all subelements in document
order, returning all elements with a matching tag. If the tree structure is modified during iteration, new or removed
elements may or may not be included. To get a stable set, use the
list() function on the iterator, and loop over the resulting list. *tag* is what tags to look for (default is to return all elements) Return an iterator containing all the matching elements. """
if tag == "*":
tag = None
if tag is None or self.tag == tag:
yield self
for e in self._children:
yield from e.iter(tag) # compatibility
def getiterator(self, tag=None):
# Change for a DeprecationWarning in 1.4
warnings.warn(
"This method will be removed in future versions. "
"Use 'elem.iter()' or 'list(elem.iter())' instead.",
PendingDeprecationWarning, stacklevel=2
)
return list(self.iter(tag)) def itertext(self):
在当前节点的子孙中根据节点名称寻找所有指定的节点的内容,并返回一个迭代器(可以被for循环)。
"""Create text iterator. The iterator loops over the element and all subelements in document
order, returning all inner text. """
tag = self.tag
if not isinstance(tag, str) and tag is not None:
return
if self.text:
yield self.text
for e in self:
yield from e.itertext()
if e.tail:
yield e.tail 节点功能
节点功能
遍历xml文档的所有内容:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Alex Li
from xml.etree import ElementTree as ET
tree = ET.parse('xo.xml')
root = tree.getroot()
print(root.attrib)
for child in root:
print(child.tag,child.attrib)
for gradechild in child:
print(gradechild.tag, gradechild.text,gradechild.attrib)
遍历xml文档的所有内容
遍历xml文档中指定的节点:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Alex Li
# 打开文件,读取XML内容
from xml.etree import ElementTree as ET
str_xml = open('xo.xml', 'r').read()
# 将字符串解析成xml特殊对象,root代指xml文件的根节点
root = ET.XML(str_xml)
# 顶层标签
print(root.tag) # 循环所有的year节点
for node in root.iter('year'):
# 将year节点中的内容自增一
new_year = int(node.text) + 1
node.text =''
# 设置属性
node.set('name', 'alex')
node.set('age', '')
# 删除属性
#del node.text
############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')
遍历xml文档中指定的节点
修改节点的内容
和上面configparser一样,所做的修改是在内存中进行,并不会对xml原始文档做修改,如果需要保存修改的配置,需要将内存里的数据写入到xml文件中。
也是分两种方式,解析字符串方式的修改保存, 和直接解析文件方式的修改,保存
解析字符串方式的修改保存
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: DBQ(Du Baoqiang) from xml.etree import ElementTree as ET # 解析xml字符串
result_xml = open('example.xml','r').read()
#找到根节点
root = ET.XML(result_xml) #顶级标签
print('顶级标签: %s'%root.tag) #循环所有的'link'节点 for i in root:
for j in i.iter('item'):
#修改原有属性:
j.text = str('http://www.jd.com') #添加属性:
j.set('linkA','https://www.dbq168.com')
j.set('linkB','http://www.sina.com.cn') #删除原有属性:
del j.attrib['linkB']
print(type(j.attrib))
print(j.attrib)
#保存内存中的修改到一个新文件,example_new.xml
tree = ET.ElementTree(root)
tree.write('example_new.xlm',encoding='utf-8') 解析字符串方式修改保存
解析字符串方式修改保存
# #直接对解析xml文档
tree = ET.parse('example.xml')
#获取根节点
root = tree.getroot() #顶级标签
print('顶级标签: %s'%root.tag) for i in root:
for j in i.iter('item'):
#修改原有属性:
j.text = str('http://www.jd.com') #添加属性:
j.set('linkA','https://www.dbq168.com')
j.set('linkB','http://www.sina.com.cn') #删除原有属性:
del j.attrib['linkB']
print(type(j.attrib))
print(j.attrib) #保存文件到example_new2.xml
tree.write('example_new2.xml',encoding='utf-8') 解析xml文件方式修改保存
解析XML文件方式修改保存
删除节点内容
for i in root:
for j in i.findall('language'):
i.remove(j)
解析字符串方式删除节点
(3) 创建xml文档
方式一
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: DBQ(Du Baoqiang) from xml.etree import ElementTree as ET #创建根节点
root = ET.Element('company') #创建节点的部门
ceo = ET.Element('manager',{'ceo':'xiaomage'}) #创建coo
coo = ET.Element('manager',{'coo':'xiaoyang'}) #创建cto
cto = ET.Element('manager',{'cto':'laowang'}) #给cto创建部门
dev = ET.Element('tech',{'dev':'xiaoli'})
ops = ET.Element('tech',{'ops':'xiaoqiang'}) cto.append(dev)
cto.append(ops) #将manager添加到root中
root.append(ceo)
root.append(coo)
root.append(cto) tree = ET.ElementTree(root)
tree.write('company.xml',encoding='utf-8',short_empty_elements=False) #默认没有缩进:
<company><manager ceo="xiaomage"></manager><manager coo="xiaoyang"></manager><manager cto="laowang"><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech></manager></company> 方式一
方式一
from xml.etree import ElementTree as ET #创建根节点
root = ET.Element('company') #创建高管
ceo = root.makeelement('manager',{'ceo':'xiaomage'})
coo = root.makeelement('manager',{'coo':'xiaoyang'})
cto = root.makeelement('manager',{'cto':'laowang'}) #创建技术部门
dev = cto.makeelement('tech',{'dev':'xiaoli'})
ops = cto.makeelement('tech',{'ops':'xiaoqiang'}) cto.append(dev)
cto.append(ops) #添加部门到根节点
root.append(ceo)
root.append(coo)
root.append(cto) tree = ET.ElementTree(root)
tree.write('company_v2.xml',encoding='utf-8',short_empty_elements=False) #默认没有缩进:
<company><manager ceo="xiaomage"></manager><manager coo="xiaoyang"></manager><manager cto="laowang"><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech></manager></company> 方式二创建
方式二
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: DBQ(Du Baoqiang) from xml.etree import ElementTree as ET #创建根节点
root = ET.Element('company') #创建高管
ceo = ET.SubElement(root,'manager',attrib={'ceo':'xiaomage'})
coo = ET.SubElement(root,'manager',attrib={'coo':'xiaoyang'})
cto = ET.SubElement(root,'manager',attrib={'cto':'laowang'})
#创建技术部门
dev = ET.SubElement(cto,'tech',{'dev':'xiaoli'})
ops = ET.SubElement(cto,'tech',{'ops':'xiaoqiang'}) cto.append(dev)
cto.append(ops) tree = ET.ElementTree(root) #生成文档对象
tree.write('company_v3.xml',encoding='utf-8',short_empty_elements=False) # #创建根节点
# root = ET.Element('company')
#
# #创建高管
# ceo = root.makeelement('manager',{'ceo':'xiaomage'})
# coo = root.makeelement('manager',{'coo':'xiaoyang'})
# cto = root.makeelement('manager',{'cto':'laowang'})
#
# #创建技术部门
# dev = cto.makeelement('tech',{'dev':'xiaoli'})
# ops = cto.makeelement('tech',{'ops':'xiaoqiang'})
#
# cto.append(dev)
# cto.append(ops)
#
# #添加部门到根节点
# root.append(ceo)
# root.append(coo)
# root.append(cto)
#
# tree = ET.ElementTree(root)
# tree.write('company_v2.xml',encoding='utf-8',short_empty_elements=False) # #创建根节点
# root = ET.Element('company')
#
# #创建节点的部门
# ceo = ET.Element('manager',{'ceo':'xiaomage'})
#
# #创建coo
# coo = ET.Element('manager',{'coo':'xiaoyang'})
#
# #创建cto
# cto = ET.Element('manager',{'cto':'laowang'})
#
# #给cto创建部门
# dev = ET.Element('tech',{'dev':'xiaoli'})
# ops = ET.Element('tech',{'ops':'xiaoqiang'})
#
# cto.append(dev)
# cto.append(ops)
#
# #将manager添加到root中
# root.append(ceo)
# root.append(coo)
# root.append(cto)
#
# tree = ET.ElementTree(root)
# tree.write('company.xml',encoding='utf-8',short_empty_elements=False) #默认没有缩进:
<company><manager ceo="xiaomage"></manager><manager coo="xiaoyang"></manager><manager cto="laowang"><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech><tech dev="xiaoli"></tech><tech ops="xiaoqiang"></tech></manager></company> 方式三创建
方式三
默认保存的xml没有缩进,特别的难看,可以按照下面方式添加缩进:
from xml.etree import ElementTree as ET
from xml.dom import minidom def prettify(string):
'''
将节点转换成字符串,并添加缩进
:param string:
:return:
'''
rough_string = ET.tostring(string,'utf-8')
reparesd = minidom.parseString(rough_string)
return reparesd.toprettyxml(indent='\t') #创建根节点
root = ET.Element('company') #创建节点的部门
ceo = ET.Element('manager',{'ceo':'xiaomage'}) #创建coo
coo = ET.Element('manager',{'coo':'xiaoyang'}) #创建cto
cto = ET.Element('manager',{'cto':'laowang'}) #给cto创建部门
dev = ET.Element('tech',{'dev':'xiaoli'})
ops = ET.Element('tech',{'ops':'xiaoqiang'}) cto.append(dev)
cto.append(ops) #将manager添加到root中
root.append(ceo)
root.append(coo)
root.append(cto) string = prettify(root) f = open('company.xml','w',encoding='utf-8')
f.write(string)
f.close() #执行结果:
<?xml version="1.0" ?>
<company>
<manager ceo="xiaomage"/>
<manager coo="xiaoyang"/>
<manager cto="laowang">
<tech dev="xiaoli"/>
<tech ops="xiaoqiang"/>
</manager>
</company> 缩进
三种创建方式都一样
3 shutil模块
shutil是一种高级的文件操作工具,可操作的包括:文件、文件夹、压缩文件的处理
4 压缩打包模块
ZipFile模块是处理zip压缩包的模块,用于压缩和解压,添加和列出压缩包的内容。ZipFile是主要的类,其过程是讲每个文件以及相关的信息作为ZipInfo对象操作到ZipFile的成员,包括文件头信息和文件内容。ZipFile模块详细介绍,点我
tarfile模块可以用于压缩/解压tar类型文件,并且支持进一步压缩tar.gz和tar.bz2文件,主要是调用open函数打开文件,并返回TarFile实例。Tarfile模块详细介绍,点我
【Python自动化运维之路Day7】的更多相关文章
- python自动化运维之路~DAY7
python自动化运维之路~DAY7 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.客户端/服务器架构 C/S 架构是一种典型的两层架构,其全称是Client/Server,即 ...
- python自动化运维之路~DAY5
python自动化运维之路~DAY5 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.模块的分类 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数 ...
- 《Python自动化运维之路》 业务服务监控(二)
文件内容差异对比方法 使用diffie模块实现文件内容差异对比.dmib作为 Python的标准库模块,无需安装,作用是对比文本之间的差异,且支持输出可读性比较强的HTML文档,与 Linux下的di ...
- Python自动化运维之路-01
python的主要应用 python的擅长领域 学python有没有前途?python的语言排名 语言选择 运维会了开发后可以干什么? python的最大优势就是什么都能做. 课程概述 毕业目标 周五 ...
- 【Python自动化运维之路Day9】Socket
socket也可以认为是套接字是一种源IP地址和目的IP地址以及源端口号和目的端口号的组合.网络化的应用程序在开始任何通讯之前都必须要创建套接字.就像电话的插口一样,没有它就没办法通讯. socket ...
- 【Python自动化运维之路Day6】
1.递归思考题,阶乘 使用递归方式(函数)计算: 1*2*3*4*5*6*7*8*9*10的值 def func(num): if num==1: return 1 return num*func(n ...
- 【Python自动化运维之路Day4】
abs() 取绝对值all() 所有为真,则为真,否则为假any() 至少有一个为真,就为真,否则为假callable() 判断函数是否可以被调用,如果可以返回True,否则返回False ...
- 【Python自动化运维之路Day2】
1. 常量命名规则 在Python中,会在变量命名上标明某变量是常量,通常采用全是大写的方式来标明,如: CONNECT= '127.0.0.1' PORT = ' 2.Python编译 python ...
- 《Python自动化运维之路》 系统基础信息模块(一)
系统性能收集模块Psutil 目录: 系统性能信息模块psutil 系统性能信息模块psutil psutil能够轻松实现获取系统运行的进程和系统利用率包括(CPU,内存,磁盘 和网络)等.主要用于系 ...
随机推荐
- mysql与oracle的存储过程有什么区别?
MySQL存储过程 (1). 格式 MySQL存储过程创建的格式:CREATE PROCEDURE过程名 ([过程参数[,...]]) [特性 ...]过程体 案例分析: 参数 MySQL存储过程的参 ...
- NRF51822之SPI
/**@brief Function for initializing a SPI master driver. * * @param[in] p_instance Pointer to SPI ma ...
- Deep Learning(深度学习)学习笔记整理
申明:本文非笔者原创,原文转载自:http://www.sigvc.org/bbs/thread-2187-1-3.html 4.2.初级(浅层)特征表示 既然像素级的特征表示方法没有作用,那怎样的表 ...
- java如何得到GET和POST请求URL和参数列表(转)
在servlet中GET请求可以通过HttpServletRequest的getRequestURL方法和getQueryString()得到完整的请求路径和请求所有参数列表,POST的需要getPa ...
- XidianOJ 1076 小W喜欢的数字
题目描述 大家都知道,小W是一名大帅哥,当然比起Light还是有点儿差距的!帅气的小W认为0-9这些数字,只有1,3,5是完美的. 欲问小W为什么,小W总是说"帅哥,是不需要解释的" ...
- Python全栈开发day3
1.Pycharm使用介绍 1.1 新建py文件自动添加python和编码 1.2 更改pycharm默认字体和风格 点击左上角“file”-->“Settings”(或者用“Ctrl+Alt+ ...
- ArcGIS Wpf MarkerSymbol 图形符号无法序列化为 JSON
[问题贴,尚不知如何解决] 在GraphicsLayer中添加一个点,使用自定义模板渲染该点,在Vs2012设计界面可以看到,但运行时出现异常 代码如下: <esri:Graphic x:Nam ...
- C++新特性(类)(转载)
C++新特性(类)里面讲的很清楚,转给大家分享一下 类机制: 类是对某一类对象的抽象:对象是某一类的实例: 类是一种复杂的数据类型,将不同类型的数据和这些数据相关的操作封装在一起的集合体: 通过一道程 ...
- POJ 2398 - Toy Storage 点与直线位置关系
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5439 Accepted: 3234 Descr ...
- HTML5和CSS3基础教程(第8版)-读书笔记(3)
第11章 用CSS 进行布局 网站设计主要有两大类型:固定宽度和响应式. 对于固定(fixed)布局,整个页面和每一栏都有基于像素的宽度.顾名思义,无论是使用移动电话和平板电脑等较小的设备查看页面,还 ...