python模块(3)
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)的更多相关文章
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- Python模块之configpraser
Python模块之configpraser 一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...
- Python模块之"prettytable"
Python模块之"prettytable" 摘要: Python通过prettytable模块可以将输出内容如表格方式整齐的输出.(对于用Python操作数据库会经常用到) 1. ...
- python 学习第五天,python模块
一,Python的模块导入 1,在写python的模块导入之前,先来讲一些Python中的概念性的问题 (1)模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质是.py ...
- windows下安装python模块
如何在windows下安装python模块 1. 官网下载安装包,比如(pip : https://pypi.python.org/pypi/pip#downloads) pip-9.0.1.tar. ...
- 安装第三方Python模块,增加InfoPi的健壮性
这3个第三方Python模块是可选的,不安装的话InfoPi也可以运行. 但是如果安装了,会增加InfoPi的健壮性. 目录 1.cchardet 自动检测文本编码 2.lxml 用于解析 ...
- Python基础篇【第5篇】: Python模块基础(一)
模块 简介 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就 ...
- python 模块加载
python 模块加载 本文主要介绍python模块加载的过程. module的组成 所有的module都是由对象和对象之间的关系组成. type和object python中所有的东西都是对象,分为 ...
- pycharm安装python模块
这个工具真的好好,真的很喜欢,它很方便,很漂亮,各种好 pycharm安装python模块:file-setting-搜索project inte OK
- Python模块常用的几种安装方式
Python模块安装方法 一.方法1: 单文件模块直接把文件拷贝到 $python_dir/Lib 二.方法2: 多文件模块,带setup.py 下载模块包,进行解压,进入模块文件夹,执行:pytho ...
随机推荐
- selenium得到弹出窗口
# 获取当前的页面窗口 first_handle = brower.current_window_handle handles = brower.window_handles for i in han ...
- nginx 启动 + uwsgi + django
https://www.cnblogs.com/chenice/p/6921727.html https://blog.csdn.net/Aaroun/article/details/78218131
- Scrapy学习笔记(5)-CrawlSpider+sqlalchemy实战
基础知识 class scrapy.spiders.CrawlSpider 这是抓取一般网页最常用的类,除了从Spider继承过来的属性外,其提供了一个新的属性rules,它提供了一种简单的机制,能够 ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
- Eureka-zookeeper的服务发现替代方案
参考: https://my.oschina.net/thinwonton/blog/1622905 http://www.open-open.com/lib/view/open14269407225 ...
- 【题解】Luogu P4450 双亲数
原题传送门 这题需要运用莫比乌斯反演(懵逼钨丝繁衍) 设F(t)表示满足gcd(x,y)%t=0的数对个数,f(t)表示满足gcd(x,y)=t的数对个数,实际上答案就是f(d) 这就满足莫比乌斯反演 ...
- Linear Regression with PyTorch
Linear Regression with PyTorch Problem Description 初始化一组数据 \((x,y)\),使其满足这样的线性关系 \(y = w x + b\) .然后 ...
- 关于A left join B,A是否一定是主表?
一般情况,我们作左连接 select * from A left join B on A.id=B.a_id;一定认为A就是主表,其实还有另外的情况,我们若将sql改写成 select * from ...
- (4opencv)对OpenCV中“旋转”的思考和实验
我记得曾经有人对OpenCV的旋转吐槽,意思是它自己没有很好的关于选择的算法.在新的版本里面添加了这些函数(我还没有时间去看是什么时候pr的).现在一个比较棘手的问题,就是OpenCV中旋转 ...
- KNN(K-Nearest Neighbor)介绍
KNN(K-Nearest Neighbor)介绍 原文地址:https://www.cnblogs.com/nucdy/p/6349172.html Wikipedia上的 KNN词条 中有一个比较 ...