今日内容详细024
序列化模块
#什么是序列化?
序列就是字符串
序列化是把其他数据类型转为json字符串的过程
#什么是反序列化?
把json字符串转为其他数据类型的过程就是反序列化
'''json字符串 json对象'''
在Python中把其他数据类型转为json需要使用json模块
json模块中四个方法
#json.dumps ---->用于将Python对象序列化为JSON格式的字符串
d={"username":'kevin','age':18}
res=json.dumps(d) #序列化成字符串格式 {"username": "kevin", "age": 18} <class 'str'>
print(res,type(res))
#json.loads -----> 用于从JSON格式的字符串中解析数据并将其转换为Python对象
res1=json.loads(res) #反序列化,返回原来的类型 {'username': 'kevin', 'age': 18} <class 'dict'>
print(res1,type(res1))
小案例:
import json
d={"username":'kevin','age':18}
#写入文件必须是字符串类型和数字类型
with open('data.txt','w',encoding='utf-8') as w1:
w1.write(json.dumps(d)) #dumps自动把字典格式序列化成字符串格式
with open('data.txt','r',encoding='utf-8') as r1:
res=json.loads(r1.read()) #反序列化,将文件中的内容读出来后,自动将文件转化为字典格式
print(res,type(res))
#json.dump -----> 用于将Python对象序列化为JSON格式并将其写入文件,接受两个参数,一个参数是序列化的python对象,有一个文件对象
res=[1,2,3,4,5,6,7]
with open('data.txt','w',encoding='utf-8') as w1:
json.dump(res,w1)
#json.load -----> 用于从文件中读取JSON数据,并将其解析为Python对象
with open('data.txt','r',encoding='utf-8') as r1:
res=json.load(r1)
print(res,type(res))
d=['你好啊','123'] #如果有中文,把参数ensure_ascii变成False
print(d,json.dumps(d,ensure_ascii=False))
#Python中哪些数据类型可以序列化为json
+---------------+-------------------+
| JSON | Python |
+===============+===================+
| object | dict |
+---------------+-------------------+
| array | list |
+---------------+-------------------+
| string | str |
+---------------+-------------------+
| number (int) | int |
+---------------+-------------------+
| number (real) | float |
+---------------+-------------------+
| true | True |
+---------------+-------------------+
| false | False |
+---------------+-------------------+
| null | None |
+---------------+-------------------+
pickle模块
pickle的使用方法跟json一摸一样,json中四个方法,pickle也是这四个方法
能够被序列化的数据类型不一样,json能够序列化的数据类型是有限的
pickle能够序列化的类型:所有数据类型
'''被pickle处理之后的数据只能在Python中使用'''
pickle序列化之后的结果是二进制的
import pickle
l=[1,2,3,4,5,6]
res=pickle.dumps(l)
print(res)
res1=pickle.loads(res)
print(res1)
l=[1,2,3,4,5,6]
with open('data.txt','wb') as w:
w.write(pickle.dumps(l))
with open('data.txt','rb') as a:
res=a.read()
print(pickle.loads(res),type(pickle.loads(res)))#[1, 2, 3, 4, 5, 6] <class 'list'>
l=[1,2,3,4,5,6]
with open('data.txt','wb') as w:
pickle.dump(l,w)
with open('data.txt','rb') as a:
res=pickle.load(a)
print(res,type(res)) #[1, 2, 3, 4, 5, 6] <class 'list'>
l=[1,2,3,4,5,6,'利好啊']
with open('data.txt','wb') as w:
pickle.dump(l,w)
with open('data.txt','rb') as a:
res=pickle.load(a)
print(res) #[1, 2, 3, 4, 5, 6] <class 'list'>
hashlib模块
#什么是摘要算法?
摘要算法又称哈希算法,散列算法,它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通过用16进制的字符串表示)
1.先指定使用的加密算法:MD5,sha系列:sha1,sha128,sha256等
import hashlib
m=hashlib.md5() #使用md5加密方式
m.update(b'123456') #指定要加密的数据
print(m.hexdigest()) #e10adc3949ba59abbe56e057f20f883e
'''不管你加密的数据有多长,得到的加密解构都是固定长度的,前提是使用同一种算法'''
怎么取出加密之后的结果呢?
username=input(':').strip()
password=input(':').strip()
m=hashlib.md5()
m.update(password.encode('utf-8'))
new_password=m.hexdigest()[0:16]
data_user='%s|%s'%(username,new_password)
with open('data.txt','w',encoding='utf-8') as w1:
w1.write(data_user)
usernames=input('username:').strip()
passwords=input('passowrd:').strip()
with open('data.txt','r',encoding='utf-8') as r1:
real_user,real_pwd=r1.read().strip().split('|')
m=hashlib.md5()
m.update(passwords.encode('utf-8')) #拿到二进制编码
new_code=m.hexdigest()[0:16]
if usernames == real_user and new_code == real_pwd:
print('sucess')
else:
print('err')
#加固定盐
username=input(':').strip()
password=input(':').strip()
m=hashlib.md5()
m.update(password.encode('utf-8'))
new_password=m.hexdigest()[0:16]
tmp='lst'
new_password+=tmp #加盐
data_user='%s|%s'%(username,new_password)
with open('data.txt','w',encoding='utf-8') as w1:
w1.write(data_user)
usernames=input('username:').strip()
passwords=input('passowrd:').strip()
with open('data.txt','r',encoding='utf-8') as r1:
real_user,real_pwd=r1.read().strip().split('|')
m=hashlib.md5()
m.update(passwords.encode('utf-8')) #拿到二进制编码
new_code=m.hexdigest()[0:16]
new_code +='lst'
if usernames == real_user and new_code == real_pwd:
print('sucess')
else:
print('err')
#加随机盐
def get_code(n):
code=''
for i in range(n):
rand_int=str(random.randint(0,9))
rand_lower=chr(random.randrange(97,122))
rand_upper=chr(random.randrange(65,90))
tmp=random.choice([rand_int,rand_lower,rand_upper])
code+=tmp
return code
username=input(':').strip()
password=input(':').strip()
random_str=get_code(4) #随机四位
password+=random_str #给密码加随机盐
m=hashlib.md5() #加密
m.update(password.encode('utf-8'))
new_password=m.hexdigest()[0:16]
data_user='%s|%s|%s'%(username,new_password,random_str)
with open('data.txt','w',encoding='utf-8') as w1:
w1.write(data_user)
usernames=input('username:').strip()
passwords=input('passowrd:').strip()
#输入的密码和文件中的密码相比较
with open('data.txt','r',encoding='utf-8') as r1:
real_user,real_pwd,random_str=r1.read().strip().split('|')
passwords+=random_str #加随机盐
m=hashlib.md5()
m.update(passwords.encode('utf-8')) #拿到二进制编码
new_code=m.hexdigest()[0:16]
if usernames == real_user and new_code == real_pwd: #文件中的密码和我登录的密码坐比较
print('sucess')
else:
print('err')