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. JAVA框架之Hibernate【Hibernate缓存详解】

    1.缓存介绍 Hibernate中提供了两级Cache,第一级别的缓存是Session级别的缓存,它是属于事务范围的缓存.这一级别的缓存由hibernate管理的,一般情况下无需进行干预:第二级别的缓 ...

  2. Asp.Net Core 集成 Hangfire 配置使用 Redis 存储

    Hangfire 官方支持 MSSQL 与 Redis(Hangfire.Pro.Redis) 两种 ,由于我的数据库是 MYSQL ,粗略查询了一下文档,现在对 .NET Core 支持的并不够好, ...

  3. Sharding-jdbc(一)分库分表理解

    1.什么是分库分表 所谓的分库分表就是数据的分片(Sharding). 2.为什么需要分库分表 因为随着公司的业务越来越大,对于现成单机单个应用瓶颈问题,对数据持久化硬盘如何进行扩容. 可以从4个方面 ...

  4. todolist增加markdown模块

    markdown编辑器 利用`markdown_js`开源库实现todolist小项目的markdown日记本功能 todolist小项目地址 之前的介绍随笔todoList markdown-js仓 ...

  5. 全网最详细的Windows系统里PLSQL Developer 64bit安装之后的一些配置(图文详解)

    不多说,直接上干货! 注意的是: 本地若没有安装Oracle服务端,Oracle server服务端64位,是远程连接,因此本地配置PLSQL Developer64位. PLSQL Develope ...

  6. JavaScript -- History

    -----042-History.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...

  7. JavaScript中该如何[更好的]做动效

    在用js写动画的时候,无非使用 setTimeout/setInterval 或者 requestAnimationFrame 来处理动画(在jquery的代码里也是这么干的),本文主要为了记录下两者 ...

  8. python 使用 matplotlib.pyplot来画柱状图和饼图

    导入包 import matplotlib.pyplot as plt 柱状图 最简柱状图 # 显示高度 def autolabel(rects): for rect in rects: height ...

  9. 拓展KMP算法详解

    拓展KMP解决的问题是给两个串S和T,长度分别是n和m,求S的每一个后缀子串与T的最长公共前缀分别是多少,记作extend数组,也就是说extend[i]表示S[i,n-1](i从0开始)和T的最长公 ...

  10. [Luogu 2656] 采蘑菇

    Description 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖和ZYR经过某条小径一次,可以采 ...