xml:

介绍:包含关于可扩展标记语言xml的函数

使用:

  • python有三种方法解析XML--SAX,DOM,以及ElementTree,由于xml技术落后,所以这里不对sax,dom介绍:

xml文本:

<?xml version="1.0"?>
<data>
<fruit name="apple">
<color>red</color>
</fruit>
<fruit name="banana">
<color>yellow</color>
</fruit>
<fruit name="pine">
<color>unknow</color>
</fruit>
</data>

ElementTree的使用:导入模块--》获取解析对象--》获取结点--》操作结点

#导入模块
import xml.etree.ElementTree as ET
#创建对应解析对象
tree=ET.parse('data.xml')
#获取结点
root=tree.getroot()#获取根节点
print(root.tag)
# print(root.getchildren())#获取子结点
#通过迭代获取子结点,“建议”
for child in root:
print(child.tag)#获取子结点的标签
print(child.attrib)#获取子结点的属性
print(child.text)#获取子结点的文本
for cc in child:#孙子结点
print(cc.tag)
print(cc.attrib)
print(cc.text)
print("-----------------") #通过标签名查找结点:
for i in root.findall('fruit'):#这个只能找子结点
print(i.tag)
print(i.attrib)
print(i.text) print("*********************")
for i in root.iter('color'):
print(i.tag)
print(i.text) ###查找结点并修改
print("*********************")
for i in root.iter('color'):
print(i.text)
if i.text=='unknow':
i.text='unknowd'#修改text #设置属性用set: i.set(属性名,属性值)
tree.write("data2.xml")#可写回原文件,也可新写来另外的文件 ###查找结点并删除
for i in root.findall('fruit'):
if i.attrib['name'] == 'pine':
root.remove(i)#从父结点删除子结点
tree.write("data3.xml")#可写回原文件,也可新写来另外的文件 ###新建结点 new_xml = ET.Element("new parent")#新建父结点 注:只能有唯一一个父结点,这相当于新建xml
son_name = ET.SubElement(new_xml, "name", attrib={"att": "yes"})#新建子结点
son_color = ET.SubElement(son_name, "color", attrib={"att": "no"})
son_color.text = 'unknow' name2 = ET.SubElement(new_xml, "name", attrib={"att": "no"})
color2 = ET.SubElement(name2, "color") et = ET.ElementTree(new_xml) # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)#写入文件,确定格式

configparser:

介绍:包括关于配置文件的函数

初始文本内容:

[DEFAULT]
ip = 192.168.1.1
hostname=aotuman [african]
color = black
luck = no

用法:

#导入模块
import configparser
#创建解析器
con=configparser.ConfigParser() print("-------读-------------")
con.read('config.ini') print(con.default_section)#默认标签的标签名
print(con.defaults())#默认标签的结果
print(con.sections())#默认标签以外的标签名
print(con['african']['color'])#获取对应标签属性 print("-------------写的方式------------")
con["DEFAULT"] = {'root':'yes'}#覆盖式修改 con['Europe'] = {'color':'white','luck':'super'}#
con['Europe']['life']='0'#增加式修改 with open('example.ini', 'w') as configfile:
con.write(configfile)#写回,或写到新文件 print("-------------增加------------")
#先判断是否存在
if con.has_section('superman')==False:
con.add_section('superman')
con['superman']={'size':'big'}
with open('example2.ini', 'w') as configfile:
con.write(configfile) # 写回,或写到新文件 print("-------------删除------------")
# con.remove_option('Europe','life')#删除选项
con.remove_section('Europe')#删除标签
with open('example3.ini', 'w') as configfile:
con.write(configfile) # 写回,或写到新文件

注意:所有的赋值都要使用字符串


hashlib:

介绍:包括关于hash加密的函数【hmac 和hashlib都是python中常用的加密模块】

用法:

#导入模块
import hashlib my_md5=hashlib.md5()##选择加密方式,创建对象
my_md5.update(b'123')#添加加密文本
my_md5.update('中文'.encode())#需要转换成bytes才能加密
print(my_md5.hexdigest())#以十六进制格式显示加密信息(常用) # ######## sha1 ######## hash = hashlib.sha1()
hash.update(b'123')
print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256()
hash.update(b'123')
print(hash.hexdigest()) # ######## sha384 ######## hash = hashlib.sha384()
hash.update(b'123')
print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512()
hash.update(b'123')
print(hash.hexdigest())

8.2、常用模块介绍2:xml,configparser,hashlib的更多相关文章

  1. 常用模块之 re shutil configparser hashlib xldt和xlwd

    shutil 高级文件处理模块 封装的更简单了 主要是文件的复制,移动,压缩解压缩 需要保证目标文件已经存在shutil.copymode('test.txt','testcopy4.txt') 压缩 ...

  2. python基础31[常用模块介绍]

    python基础31[常用模块介绍]   python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...

  3. Ansible常用模块介绍及使用(week5_day1_part2)--技术流ken

    Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)--技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几 ...

  4. Ansible常用模块介绍及使用(2)

    Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)–技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几个 ...

  5. python模块(shelve,xml,configparser,hashlib,logging)

    1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...

  6. python3之xml&ConfigParser&hashlib&Subprocess&logging模块

    1.xml模块 XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存储 ...

  7. Python模块 shelve xml configparser hashlib

    常用模块1. shelve 一个字典对象模块 自动序列化2.xml 是一个文件格式 写配置文件或数据交换 <a name="hades">123</a>3. ...

  8. IIS7 常用模块介绍说明

    1.1.0   IIS常用的功能模块介绍: 1)         静态内容:可发布静态 Web 文件格式,比如 HTML 页面和图像文件. 2)         默认文档:允许您配置当用户未在 URL ...

  9. python 2.0 s12 day5 常用模块介绍

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

随机推荐

  1. Twitter Bootstrap3小结

    今天有空,小结一下Twitter Bootstrap 3的使用.首先不得不说,Bootstrap是迄今(2014)比较好的WEB设计框架(当然,其它的优秀WEB Framework还有:Foundat ...

  2. jmeter接口自动化部署jenkins教程

    首先,保证本地安装并部署了jenkins,jmeter,xslproc 我搭建的自动化测试框架是jmeter+jenkins+xslproc ---注意:原理是,jmeter自生成的报告jtl文件,通 ...

  3. 浏览器对CSS小数点的解析——坑

    在写移动端项目时,为了将一个元素垂直居中,于是我将元素的高和行高设置成一样的,但是显示出来的结果,却让人不得其解,如下: 可以看到按钮的底部有一条缝隙,一开始以为是代码写错了,于是检查了一下,发现没啥 ...

  4. git第一节----git config配置

    @查看git的版本 git --version @查看git配置信息 git config --list config list分全局和局部,在根目录下执行git config --list显示为全局 ...

  5. 性能提速:debounce(防抖)、throttle(节流/限频)

    debounce与throttle是用户交互处理中常用到的性能提速方案,debounce用来实现防抖动,throttle用来实现节流(限频).那么这两个方法到底是什么(what)?为何要用(why-解 ...

  6. LR监测windows资源一般监测哪几个项?

    计数器        指标 1.        平均事务响应时间 Average Transation Response Time        优秀:<2s 良好:2-5s 及格:6-10s ...

  7. 并发编程之 wait notify 方法剖析

    前言 2018 元旦快乐. 摘要: notify wait 如何使用? 为什么必须在同步块中? 使用 notify wait 实现一个简单的生产者消费者模型 底层实现原理 1. notify wait ...

  8. 一:MyBatis知识整理(1)

    一:MyBatis的架构 1.mybatis配置SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息. mapper.xml文件即sql映射文 ...

  9. 新版TeamTalk部署教程

    新版TeamTalk部署教程 新版TeamTalk已经在2015年03月28日发布了,目前版本定为1.0.0版本,后续版本号会按照如下规则进行:1.版本规则按照x.y.z的形式进行.2.各端小bug修 ...

  10. The area (hdu1071)积分求面积

    The area Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...