一、xml模块
<?xml version="1.0"?>
-<data>
-<country name="Liechtenstein" a="1">
<rank updated="yes">2</rank>
<year updated="yes" version="1.0">2010</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
<egon age="18">hello</egon>
</country>
-<country name="Singapore">
<rank updated="yes">5</rank>
<year updated="yes" version="1.0">2013</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
<egon age="18">hello</egon>
</country>
-<country name="Panama">
<year updated="yes" version="1.0">2013</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
<egon age="18">hello</egon>
</country>
</data>
import xml.etree.ElementTree as ETtree=ET.parse(‘a.xml‘)root=tree.getroot() for child in root: print(child) for i in child: print(i.tag,i.attrib,i.text) #标签名,属性,内容 years=root.iter(‘year’) #扫描整个xml文档树,找到所有for i in years: print(i)------------------------------------------------------------------- res1=root.find(‘country’) #谁来调,就从谁下一层开始找,只找一个print(res1)res2=root.findall(‘country’) #谁来调,就从谁下一层开始找,找出全部 print(res2)
------------------------------------------------------------------- 修改years=root.iter(‘year’)for year in years: year.text=str(int(year.text)+1) #将year的内容加1 year.set(‘updated’,‘yes’) #将year标签添加属性 year.set(‘version’,‘1.0’) tree.write(‘a.xml’)
------------------------------------------------------------------- 删除
for county in root.iter(‘country‘): #找到所有country # print(county.tag) rank=county.find(‘rank‘) #查看country下面有没有rank if int(rank.text) > 10: #删除rank内容大于10的 county.remove(rank)tree.write(‘a.xml‘)-------------------------------------------------------------------增加节点
for county in root.iter(‘country‘): e=ET.Element(‘egon‘) #添加egon的标签 e.text=‘hello‘ e.attrib={‘age‘:‘18‘} county.append(e) tree.write(‘a.xml‘) 二、configparser模块
[egon]
name = egon
age = 18
is_admin = True
salary = 3.1
[alex]
name = alex
age = 38
is_admin = False
取配置
print(config.sections()) #看标题
print(config.options(config.sections()[0])) #查看某个标题下的配置项
res=config.get(‘alex‘,‘name‘)#查看某个标题下的某个配置项的值print(type(res))
res1=config.getint(‘egon‘,‘age‘)#查看某个标题下的某个配置项的整数值print(type(res1))res1=config.getboolean(‘egon‘,‘is_admin‘)#查看某个标题下的某个配置项的布偶值print(type(res1))res1=config.getfloat(‘egon‘,‘salary‘)#查看某个标题下的某个配置项的浮点值print(type(res1))
修改config.remove_section(‘alex‘) #删除alex标题config.remove_option(‘egon‘,‘age‘) #删除egon标题下的age配置config.add_section(‘alex‘) #添加alex标题config.set(‘alex‘,‘name‘,‘SB‘) #添加alex标题下name配置项,内容为SBconfig.write(open(‘a.ini‘,‘w‘)) #保存 三、hashlib模块hash:一种算法,3.x里代替了md5模块和sha模块三个特点:1.内容相同则hash运算结果相同,内容稍微改变则hash值则变2.不可逆推3.相同算法:无论校验多长的数据,得到的哈希值长度固定
import hashlib
m=hashlib.md5()
m.update(‘hello‘.encode(‘utf-8‘))m.update(‘world‘.encode(‘utf-8‘))print(m.hexdigest()) m=hashlib.md5()m.update(‘helloworld‘.encode(‘utf-8‘))print(m.hexdigest()) m=hashlib.md5(‘helloworld‘.encode(‘utf-8‘))print(m.hexdigest()) m=hashlib.md5(‘h‘.encode(‘utf-8‘))m.update(‘elloworld‘.encode(‘utf-8‘))print(m.hexdigest())
m=hashlib.md5()with open(‘a.xml‘,‘rb‘) as f: for line in f: m.update(line)print(m.hexdigest()) 耗费内存不推荐使用,文件大会撑爆内存m=hashlib.md5()with open(‘a.xml‘,‘rb‘) as f: m.update(f.read())print(m.hexdigest())
加盐password=‘alex3714‘m=hashlib.md5(‘yihangbailushangqingtian‘.encode(‘utf-8‘))m.update(password.encode(‘utf-8‘))passwd_md5=m.hexdigest()print(passwd_md5)
import hmac必须保证初始值一样,hash值才一样,也就是必须都是hello,强制加盐h=hmac.new(‘hello‘.encode(‘utf-8‘))h.update(‘world‘.encode(‘utf-8‘))print(h.hexdigest()) h=hmac.new(‘hello‘.encode(‘utf-8‘))h.update(‘w‘.encode(‘utf-8‘))h.update(‘or‘.encode(‘utf-8‘))h.update(‘ld‘.encode(‘utf-8‘))print(h.hexdigest()) 四、subprocess模块
import subprocess res=subprocess.Popen(r‘deeddddir D:\04-视频录制存放目录\python18期\day7\xml模块‘, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)print(‘-========>‘,res.stderr.read().decode(‘gbk‘))
#dir file_path | findstr xml$res1=subprocess.Popen(r‘dir D:\04-视频录制存放目录\python18期\day7\xml模块‘, shell=True, stdout=subprocess.PIPE,)# stdin=res1.stoutres2=subprocess.Popen(r‘findstr xml$‘, shell=True, stdin=res1.stdout, stdout=subprocess.PIPE,)print(res2.stdout.read().decode(‘gbk‘))