python模块(3)

时间:2023-03-09 09:56:39
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())