1.xml:实现不同语言或程序之间进行数据交换的协议

<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>

遍历、修改、删除结点

 import xml.etree.ElementTree as ET

 tree = ET.parse("xml_test.xml")
root = tree.getroot()
print(root.tag) # # 遍历xml文档
# for child in root:
# print(child.tag, child.attrib)
# for i in child:
# print(i.tag, i.text)
#
# # 只遍历year 节点
# for node in root.iter('year'):
# print(node.tag, node.text) # 修改node
# for node in root.iter('year'):
# new_year=int(node.text)+1
# node.text=str(new_year)
# node.set('update','yes')
#
# tree.write('xml_test.xml') # 删除node
for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank>50:
root.remove(country) tree.write('re_xml.xml')

创建

 import xml.etree.ElementTree as ET

 new_xml = ET.Element("namelist")

 person1= ET.SubElement(new_xml, "person1", attrib={"enrolled": "yes"})
name = ET.SubElement(person1, "name")
age = ET.SubElement(person1, "age", attrib={"checked": "no"})
sex = ET.SubElement(person1, "sex")
name.text = 'Alex'
age.text = ''
sex.text = 'man' person2 = ET.SubElement(new_xml, "person2", attrib={"enrolled": "no"})
name = ET.SubElement(person2, "name")
age = ET.SubElement(person2, "age")
sex = ET.SubElement(person2, "sex")
name.text = 'Bob'
age.text = ''
sex.text = 'man' et = ET.ElementTree(new_xml) # 生成文档对象
et.write("xml_create.xml", encoding="utf-8", xml_declaration=True) ET.dump(new_xml) # 打印生成的格式

2.configparser:用于生成和修改常见配置文档

生成

 import configparser

 #创建configparse对象
config = configparser.ConfigParser()
#每个section都是一个字典
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg' config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = ''
topsecret['ForwardX11'] = 'no' config['DEFAULT']['ForwardX11'] = 'yes'
with open('ha1.ini', 'w') as f:
config.write(f)

读取

 import configparser
#配置文件的读取与更改 #创建configparse对象
conf=configparser.ConfigParser() conf.read('ha1.ini')
# print(conf.sections())
# print(conf.defaults())
print(conf['bitbucket.org']['user']) conf.remove_section('bitbucket.org') with open('conf.ini','w') as f:
conf.write(f)

3.hashlib

import hashlib
# 加密的消息要以bytes类型传入相应的对象中 #生成md5对象
m = hashlib.md5()
#调用对象的update方法
m.update('Hello'.encode(encoding='utf-8')) #str的encode方法,等价于转为bytes类型
print(m.hexdigest())
m.update(b"It's me")
print(m.hexdigest()) #第二次生成的是所有内容的密文 # 生成sha512对象
s1=hashlib.sha256()
s1.update('Hello这是一个测试'.encode(encoding='utf-8'))
print(s1.hexdigest())

4.hmac

import hmac
#对key和msg进行处理后再加密,安全性更高
#无论是密钥key,还是明文msg,一定要以bytes类型传入
m=hmac.new('family_861'.encode(encoding='utf-8'),'我要测试'.encode(encoding='utf-8')) #key,msg都转换成bytes类型
print(m.digest())
print(m.hexdigest())

python模块(3)的更多相关文章

  1. 使用C/C++写Python模块

    最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...

  2. Python模块之configpraser

    Python模块之configpraser   一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...

  3. Python模块之"prettytable"

    Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...

  4. python 学习第五天,python模块

    一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...

  5. windows下安装python模块

    如何在windows下安装python模块 1. 官网下载安装包,比如(pip : https://pypi.python.org/pypi/pip#downloads) pip-9.0.1.tar. ...

  6. 安装第三方Python模块,增加InfoPi的健壮性

    这3个第三方Python模块是可选的,不安装的话InfoPi也可以运行. 但是如果安装了,会增加InfoPi的健壮性. 目录 1.cchardet    自动检测文本编码 2.lxml    用于解析 ...

  7. Python基础篇【第5篇】: Python模块基础(一)

    模块 简介 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就 ...

  8. python 模块加载

    python 模块加载 本文主要介绍python模块加载的过程. module的组成 所有的module都是由对象和对象之间的关系组成. type和object python中所有的东西都是对象,分为 ...

  9. pycharm安装python模块

    这个工具真的好好,真的很喜欢,它很方便,很漂亮,各种好 pycharm安装python模块:file-setting-搜索project inte OK

  10. Python模块常用的几种安装方式

    Python模块安装方法 一.方法1: 单文件模块直接把文件拷贝到 $python_dir/Lib 二.方法2: 多文件模块,带setup.py 下载模块包,进行解压,进入模块文件夹,执行:pytho ...

随机推荐

  1. php 使用fileupload上传多张图片,压缩包

    test.php <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. (4运行例子)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

    ​1.联通ColaB 2.运行最基础mnist例子,并且打印图表结果  # https://pypi.python.org/pypi/pydot#!apt-get -qq install -y gra ...

  3. Spring 学习历程(二)

    JUnit测试 maven导入包 <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> &l ...

  4. noip模拟【tea】

    tea [题目描述]有n个容量为V的瓶子,第i个瓶子中装着a[i]个单位的tea,使所有瓶子内的tea在不 超过其容量的前提下,非空的瓶子最少.在一个单位时间内,可以同时将多个瓶子中的tea倒入另外多 ...

  5. 外观模式Facade pattern

    http://www.runoob.com/design-pattern/facade-pattern.html 外观模式 外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一 ...

  6. iis6-0 cve-2017-7269 批量验证脚本

    代码地址 import subprocess f = open('ips.txt', 'r') flines = f.readlines() vulnsrvs = 0 i = 1 for line i ...

  7. js插入排序

    插入排序 平均时间复杂度O(n*n) 最差情况O(n*n) 最好情况O(n) 空间复杂度O(1) 稳定性:稳定 function insertSort (arr) { var len = arr.le ...

  8. GDOI2018D2T1 谈笑风生

    T1 谈笑风生 [题目描述] [输入] [输出] 一行两个数,所需能量P与在能量最小的前提下最短的到达时间t. [样例输入] 5 7 66 4 3 2 1 5 1 2 1 5 2 3 2 4 2 5 ...

  9. (转) K-Means聚类的Python实践

    本文转自: http://python.jobbole.com/87343/ K-Means聚类的Python实践 2017/02/11 · 实践项目 · K-means, 机器学习 分享到:1 原文 ...

  10. 1.2成员变量+类变量+static关键字

    成员变量和类变量的区别 由static修饰的变量称为静态变量,其实质上就是一个全局变量.如果某个内容是被所有对象所共享,那么该内容就应该用静态修饰:没有被静态修饰的内容,其实是属于对象的特殊描述. 不 ...