一:hashlib 模块
1,什么是hash
hash是一种算法,该算法接收传入的内容,经过运算得到一串的hash值,如果把hash算法比喻为一座工厂
那传给hash算法的内容就是原材料,生成的hash值就是生产出的产品
2,为何要用hash?
hash值/产品有三大特征:
1:只要传入的内容一样,得到的hash值必然是一样的
2:只要我们使用的hash算法固定,无论传入的内容有多大,得到的hash值的长度是固定
3:不可以用hash值逆退出原来的内容
基于1,2可以在下载文件时做文件的一致性校验
基于1,3可以对密码进行加密
# 应用一:对明文密码进行加密
# password=input('>>>: ')
#
# m=hashlib.md5()
# m.update('天王盖地虎'.encode('utf-8'))#密码加盐
# m.update(password.encode('utf-8'))
# print(m.hexdigest()) #95bd6eafefdf51d8b153785f3fb6263d
3,用法?
1,造出hash工厂 m=hashlib.md5()
2,运送原材料
m.update('你好世界‘.encode('utf-8'))
m.update('pw'.encode(;utf-8')) #可以运送多次hash值,只要传入的值一样得到的hash值就是一样
3,产出hash值
print(m.hexdigest())#拿到hash值可以进行操作
二:shutill模块
1,拷贝文件
import shutil
# with open('old.xml','r') as read_f,open('new.xml', 'w') as write_f:
# shutil.copyfileobj(read_f,write_f)#拷贝文件
2,压缩文件
import tarfile
t=tarfile.open('data_bak.tar.gz','r')
t.extractall('D:\SH_fullstack_s2\day20\dir')
t.close()
三:sheleve模块
序列化文件
import shelve
# dic1={'pwd':'alex3714','age':18,'sex':'male'}
# dic2={'pwd':'alex3715','age':73,'sex':'male'}
d=shelve.open('db.txt',writeback=True)
# # d['egon']=dic1
# # d['alex']=dic2
# d['egon']['age']=19
print(d['egon'])
d.close()
四:xml模块
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
root = tree.getroot()
# 对于任何标签都有三个特征:标签名、标签属性、标签的文本内容
# print(root.tag)
# print(root.attrib)
# print(root.text)
# print(list(root.iter('year'))) #全文搜索,找到所有
# for year in root.iter('year'):
# print(year.tag)
# print(year.attrib)
# print(year.text)
# print('='*100)
# print(root.find('country').attrib) #在root的子节点找,只找一个
# print([country.attrib for country in root.findall('country')]) #在root的子节点找,找所有
## 1、查
#遍历整个文档
# for country in root:
# print('============>国家 %s' %country.attrib)
# for item in country:
# print(item.tag)
# print(item.attrib)
# print(item.text)
#2、改
# for year in root.iter('year'):
# print(year.tag)
# year.attrib={'updated':'yes'}
# year.text=str(int(year.text)+1)
# tree.write('a.xml')
#3、增
# for country in root:
# rank=country.find('rank')
# if int(rank.text) > 50:
# # print('符号条的国家',country.attrib)
# tag=ET.Element('egon')
# tag.attrib={'updated':'yes'}
# tag.text='NB'
# country.append(tag)
# tree.write('a.xml'
#4、删
for country in root:
tag=country.find('egon')
# print(tag,bool(tag))
if tag is not None:
print('====>')
country.remove(tag)
tree.write('a.xml')