8.2、常用模块介绍2:xml,configparser,hashlib
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的更多相关文章
- 常用模块之 re shutil configparser hashlib xldt和xlwd
shutil 高级文件处理模块 封装的更简单了 主要是文件的复制,移动,压缩解压缩 需要保证目标文件已经存在shutil.copymode('test.txt','testcopy4.txt') 压缩 ...
- python基础31[常用模块介绍]
python基础31[常用模块介绍] python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的li ...
- Ansible常用模块介绍及使用(week5_day1_part2)--技术流ken
Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)--技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几 ...
- Ansible常用模块介绍及使用(2)
Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)–技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几个 ...
- python模块(shelve,xml,configparser,hashlib,logging)
1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...
- python3之xml&ConfigParser&hashlib&Subprocess&logging模块
1.xml模块 XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存储 ...
- Python模块 shelve xml configparser hashlib
常用模块1. shelve 一个字典对象模块 自动序列化2.xml 是一个文件格式 写配置文件或数据交换 <a name="hades">123</a>3. ...
- IIS7 常用模块介绍说明
1.1.0 IIS常用的功能模块介绍: 1) 静态内容:可发布静态 Web 文件格式,比如 HTML 页面和图像文件. 2) 默认文档:允许您配置当用户未在 URL ...
- python 2.0 s12 day5 常用模块介绍
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
随机推荐
- CentOS 6.7 下 MYSQL 5.7 的安装与配置
安装 #yum源 http://dev.mysql.com/downloads/repo/yum/ #安装 rpm -Uvh http://dev.mysql.com/get/mysql57-comm ...
- Vue + Element UI 实现权限管理系统 前端篇(四):优化登录流程
完善登录流程 1. 丰富登录界面 1.1 从 Element 指南中选择组件模板丰富登录界面,放置一个登录界面表单,包含账号密码输入框和登录重置按钮. <template> <el- ...
- mysql-定时对表分区
1, 分区 具体可见: http://blog.csdn.net/open_data/article/details/46893331 1, 分区类型: RANGE分区:基于属于一个给定连续区间的列值 ...
- java监听器、定时器的使用
1.监听器 在web.xml配置 <!-- 时间任务 --> <listener> <listener-class> com.hk.common.timer.Tim ...
- 一次Linux服务器被入侵和删除木马程序的经历
转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wzlinux.blog.51cto.com/8021085/1740113 一.背景 晚上看到有台服 ...
- Tomcat学习总结(14)—— Tomcat常见面试题
一.Tomcat的缺省是多少,怎么修改 Tomcat的缺省端口号是8080. 修改Tomcat端口号: 1.找到Tomcat目录下的conf文件夹 2.进入conf文件夹里面找到server.xml文 ...
- 使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件
写在前面 之前写过一篇关于vue实现dialog会话框组件的文章http://www.cnblogs.com/fozero/p/8546883.html, 讲到了如何实现一个vue对话框组件,其中涉及 ...
- SpringMVC源码阅读:属性编辑器、数据绑定
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...
- How to describe the wind sprial in computer system?
How to describe the wind sprial in computer system? 2017-02-21 刘崇军 风螺旋线 If we want get the approval ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...