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>
遍历、修改、删除结点
1 import xml.etree.ElementTree as ET
2
3 tree = ET.parse("xml_test.xml")
4 root = tree.getroot()
5 print(root.tag)
6
7 # # 遍历xml文档
8 # for child in root:
9 # print(child.tag, child.attrib)
10 # for i in child:
11 # print(i.tag, i.text)
12 #
13 # # 只遍历year 节点
14 # for node in root.iter('year'):
15 # print(node.tag, node.text)
16
17 # 修改node
18 # for node in root.iter('year'):
19 # new_year=int(node.text)+1
20 # node.text=str(new_year)
21 # node.set('update','yes')
22 #
23 # tree.write('xml_test.xml')
24
25 # 删除node
26 for country in root.findall('country'):
27 rank = int(country.find('rank').text)
28 if rank>50:
29 root.remove(country)
30
31 tree.write('re_xml.xml')
创建
1 import xml.etree.ElementTree as ET
2
3 new_xml = ET.Element("namelist")
4
5 person1= ET.SubElement(new_xml, "person1", attrib={"enrolled": "yes"})
6 name = ET.SubElement(person1, "name")
7 age = ET.SubElement(person1, "age", attrib={"checked": "no"})
8 sex = ET.SubElement(person1, "sex")
9 name.text = 'Alex'
10 age.text = '33'
11 sex.text = 'man'
12
13 person2 = ET.SubElement(new_xml, "person2", attrib={"enrolled": "no"})
14 name = ET.SubElement(person2, "name")
15 age = ET.SubElement(person2, "age")
16 sex = ET.SubElement(person2, "sex")
17 name.text = 'Bob'
18 age.text = '19'
19 sex.text = 'man'
20
21 et = ET.ElementTree(new_xml) # 生成文档对象
22 et.write("xml_create.xml", encoding="utf-8", xml_declaration=True)
23
24 ET.dump(new_xml) # 打印生成的格式
2.configparser:用于生成和修改常见配置文档
生成
1 import configparser 2 3 #创建configparse对象 4 config = configparser.ConfigParser() 5 #每个section都是一个字典 6 config["DEFAULT"] = {'ServerAliveInterval': '45', 7 'Compression': 'yes', 8 'CompressionLevel': '9'} 9 10 config['bitbucket.org'] = {} 11 config['bitbucket.org']['User'] = 'hg' 12 13 config['topsecret.server.com'] = {} 14 topsecret = config['topsecret.server.com'] 15 topsecret['Host Port'] = '50022' 16 topsecret['ForwardX11'] = 'no' 17 18 config['DEFAULT']['ForwardX11'] = 'yes' 19 with open('ha1.ini', 'w') as f: 20 config.write(f)
读取
1 import configparser 2 #配置文件的读取与更改 3 4 #创建configparse对象 5 conf=configparser.ConfigParser() 6 7 conf.read('ha1.ini') 8 # print(conf.sections()) 9 # print(conf.defaults()) 10 print(conf['bitbucket.org']['user']) 11 12 conf.remove_section('bitbucket.org') 13 14 with open('conf.ini','w') as f: 15 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())