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. apache的rewrite机制配置

    步骤: 1:启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_rewr ...

  2. kivy 笔记

    没有引入App对象,就不会得到一个窗口. kvlanguage用来构建UI界面,这个文件保存成”.kv”. kivy用widget来描述UI元素,lable.layout等都是widget 简单一点的 ...

  3. Docker学习笔记之docker volume 容器卷的那些事(二)

    预览目录 更改目录拥有者 Data Container 切换用户 参考文章 0x00 概述 如果你读了docker volume 容器卷的那些事(一),我想应该不会遇到下面这些问题的,毕竟是具有指导意 ...

  4. c++ linux下输出中文

    同样,使用的是VS FOR LINUX进行测试. converting to execution character set: Invalid or incomplete multibyte or w ...

  5. nginx动静态分离以及配置https(安全组强行切换以及导致的问题解决)

    公司原来的网络采用http/https同时支持的方式,http并不会强制自动跳转到https,最近要求强制切换,导致了一系列问题.趁今天测试完成了,整理如下: 1.要求HTTP自动跳转到HTTPS: ...

  6. Systen,IO

    private void CreateHtml(string sPath, string txt) { string currPath = @"C:\MyCodeHelper" + ...

  7. Java程序员必备的Intellij插件(长期更新,截止到2018-05-03)

    善用Intellij插件可大幅提升我们的效率 以下是我用过不错的Intellij插件 1. .ignore 生成各种ignore文件,一键创建git ignore文件的模板,免得自己去写 截图:   ...

  8. android之自定义弹框

    step1 创建窗体 final AlertDialog dialog =new Builder(this).create(); step2 获取View View viewDialog =View. ...

  9. 上传代码到github的步骤

    在你的电脑上装好git 大致流程是: 1.在github上创建项目 2.使用git clone https://github.com/xxxxxxx/xxxxx.git克隆到本地 3.编辑项目 4.g ...

  10. tp框架中的一些疑点知识-2

    tp中有三种常量: 预定义常量, 这个设置后不会随环境的改变而改变的,比如'URL_MODEL' => 1 注意是 model, 不是 url_mode 路径常量, 也不会随环境的改变而改变的, ...