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. MySQL笔记(2)---InnoDB存储引擎

    1.前言 本节记录InnoDB的相关知识点. 2.InnoDB存储引擎简介 2.1版本 MySQL5.1开始,允许用动态方式加载引擎,这样存储引擎的更新可以不受MySQL数据库版本的限制.下面是各个I ...

  2. ThreadLocal是否会引发内存泄露的分析 good

    这篇文章,主要解决一下疑惑: 1. ThreadLocal.ThreadLocalMap中提到的弱引用,弱引用究竟会不会被回收? 2. 弱引用什么情况下回收? 3. JAVA的ThreadLocal和 ...

  3. CentOS7下搭建FastDfs(V5.11)+Keepalived分布式集群部署

    FastDfs介绍 http://kb.cnblogs.com/page/82280/ 1.准备 系统 CentOS7 最小化安装. #VIP虚拟IP 10.1.6.218 #Keepalived 1 ...

  4. Chrome 的 Material Design Refresh UI初探

    今天Chrome自动升级到69.0.3497.92, 发现UI已经变成了"Material Design Refresh". Chrome 浏览器的页面标签已经不再像以往那样倾斜和 ...

  5. 自制“低奢内”CSS3注册表单,包含JS验证哦。请别嫌弃,好吗?。

    要求 必备知识 基本了解CSS语法,初步了解CSS3语法知识.和JS/JQuery基本语法. 开发环境 Adobe Dreamweaver CS6 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到 ...

  6. JavaScript 内存泄露以及如何处理

    一.前言 一直有打算总结一下JS内存泄露的方面的知识的想法,但是总是懒得提笔. 富兰克林曾经说过:懒惰,像生鏽一样,比操劳更能消耗身体,经常用的钥匙总是亮闪闪的.安利一下,先起个头. 二.内存声明周期 ...

  7. vue-cli + element-ui + webpack 新建项目

    1.进入项目目录 2.全局安装vue-cli 输入命令:npm install vue-cli -g 若报错:ENOENT: no such file or directory, access.... ...

  8. 【angular5项目积累总结】文件上传

    <div class="form-group row"> <label class="col-sm-2 col-form-label"> ...

  9. python学习之内存机制

    不可变对象(字符串.元组) 1. a = 1 首先在内存中创建对象1,并记录对象的引用计数为1次. id(a) 查看变量a引用的对象的内存地址 2. b = 1 内存中已存在对象1,变量b引用对象1, ...

  10. 表格列mouse经过时高亮显示

    前几天Insus.NET有练习<表格行mouse经过时高亮显示>http://www.cnblogs.com/insus/p/3715733.html ,今天有奇想,是否可以实现mouse ...