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. ansible中常用模块详解

    ansible中常用的模块详解: file模块 ansible内置的可以查看模块用法的命令如下: [root@docker5 ~]# ansible-doc -s file - name: Sets ...

  2. VMware环境安装MacOS

    环境: win10专业版 VMware 14 Pro 开始吧 1. 停止服务 2. 解压并管理员权限运行unlocker,目的是使得 win10 环境下的 VMWare14Pro 支持 mac 系统的 ...

  3. P3804 【模板】后缀自动机

    P3804 [模板]后缀自动机 后缀自动机模板 详情可见luogu题解板块 #include<iostream> #include<cstdio> #include<cs ...

  4. P1357 花园

    洛咕原题 题解 状压dp+矩乘 首先看到题目说M<=5,这么小的数据明显可以用状压保存相邻状态,于是可以得到一个80分的dp: 先筛出所有可用的状态,然后建立一个矩阵保存可转移的状态,再然后把每 ...

  5. 选择排序法、冒泡排序法、插入排序法、系统提供的底层sort方法排序之毫秒级比较

    我的代码: package PlaneGame;/** * 选择排序法.冒泡排序法.插入排序法.系统提供的底层sort方法排序之毫秒级比较 * @author Administrator */impo ...

  6. bzoj 3670 动物园 - kmp - 动态规划

    Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习 ...

  7. 【Python026--字典内键方法】

    一,内键方法 1.fromkeys(...) 语法:dict1.fromkeys(s[,v]):s指的是字典的键值,[,v]指的是可选项(值),[,v]不填写的话默认为none #不填写第二个值,默认 ...

  8. topcoder srm 693 div1 -3

    1.给出一个$n$个顶点的无向带权图.其中顶点$i,i+1$之间存在边,$i,i+2$之间存在边.而且仅有这些边.现在删掉其中的一些边,剩下的边满足图仍然是2联通的情况下使得权值和最小? 思路:其实就 ...

  9. 第三章 Web页面建设

    认识<q>元素: 简短的引用. 问:你去掉了双引号,换成了一个<q>元素,只是为了显示双引号?这样不是更复杂了吗? 答:不.在增加<q>元素之前,浏览器只知道这是一 ...

  10. tp框架中的一些疑点知识--cookie和session的配置

    不同的浏览器采用不同的方式保存Cookie. IE浏览器会在"C:\Documents and Settings\你的用户名\Cookies"文件夹下以文本文件形式保存,一个文本文 ...