Python提供了很多序列化模块,其中比较常用的有pickle、json。
json序列化其实就是字符串,序列化就是把其他数据类型转化为json字符串的过程
返序列化就是把json字符串转化为其他数据类型
d ={"name": "coco", "age": 19}
'''以上就是json的序列化模式,注意是双引号'''
d1 = {'name': 'coco',' "age"': 19}
'''这是字典,单引号,与上面的json序列化的数据类型不同,要注意'''
print(type(d1))##
1.解释器的字典
d = {'name':'ki','pwd':1253}
print(d,type(d)) # {'name': 'ki', 'pwd': 1253}
2.json模块>>>是字符串
import json # 引入json 模块
result = json.dumps(d) # 把字典用json 读取成字符串
print(result,type(result)) # {"name": "ki", "pwd": 1253}
针对数据
json.dumps()
json.loads()
针对文件
json.dump()
json.load()
+-------------------+---------------+
| Python | JSON |
+===================+===============+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str | string |
+-------------------+---------------+
| int, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | false |
+-------------------+---------------+
| None | null |
+-------------------+---------------+
import json
d ={"name": "coco", "age": 19}
with open('a.txt', 'w', encoding='utf-8')as f:
f.write(json.dumps(d))
with open('a.txt', 'r', encoding='utf-8')as f1:
res = eval(f1.read())
print(res) ##{'name': 'coco', 'age': 19}
"""反序列化"""
with open('a.txt', 'r', encoding='utf-8')as f3:
res1 = f3.read()
res2 = json.loads(res1)##返序列化
print(res2) ##{'name': 'coco', 'age': 19}
d = {'username':'jerry你好啊', 'age': 18}
print(json.dumps(d, ensure_ascii=False))
"""{"username": "jerry你好啊", "age": 18}"""
pickle的使用方式跟json一模一样,json中有四个方法,pickle也是这四个方法,但是他与json的区别就是pickie可以将所有的数据类型序列化,但是只能在python中使用,而json能够序列化的数据类型:dict,list,tuple,int,float,pickie序列化之后的结果是二进制。
import pickle
d = [1, 2, 3, 4]
print(pickle.dumps(d))
##b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00]\x94(K\x01K\x02K\x03K\x04e.'
写入文件的内容不可以修改,怎么存的就怎么取出
import pickle d = [1, 2, 3, 4] print(pickle.dumps(d)) ##b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00]\x94(K\x01K\x02K\x03K\x04e.' with open('a.txt', 'wb')as f: pickle.dump(d, f) with open('a.txt', 'rb')as f1: print(pickle.load(f1))##[1, 2, 3, 4]
摘要算法:又称哈希算法,散列算法。通过一个函数把任意长度的数据转换长度固定的数据串(通常使用十六进制表示)
算法:MD5, sha系列,sha1,sha128,sha256等
m = hashlib.md5()
指定加密数据:
m.update(b)
不管输入加密数据有多长,得到的加密串结构是固定的。
取出:
import hashlib m = hashlib.md5() m.update(b'1223l') print(m.hexdigest()) ##5dffa7458bdc04ddc4abde1ae0c58db2
实例:
加密密码的注册与登录
##加密注册
import hashlib
user_name = input('username:')
password = input('password')
s_password = password + 'qaz'
m = hashlib.md5() ##进行加密
m.update(s_password.encode('utf-8'))
new_pwd = m.hexdigest()[0:16]
date = '%s|%s' % (user_name, new_pwd)
with open('username.txt','w',encoding='utf-8')as f:
f.write(date)
##登录系统
user_name = input('输入登录账号:')
password = input('输入密码')
s_password = password + 'qaz'
m = hashlib.md5()
m.update(s_password.encode('utf-8'))
new_pwd = m.hexdigest()[0:16]
with open('username.txt', 'r', encoding='utf-8')as f:
real_name, real_pwd = f.read().split('|')
if user_name == real_name and real_pwd == new_pwd:
print('登录成功')
else:
print('账号或密码错误')
随机加密数与多用户注册登录
import hashlib
import random
def yz(n):
code = ''
for i in range(n):
new = str(random.randint(1, 10))
random_upper = chr(random.randint(65, 90))
random_loree = chr(random.randint(97, 122))
code += random.choice([new, random_upper, random_loree])
return code
res = yz(5)
while True:
username = input('注册')
password = input('请输入密码')
new_pwd = password + res
m = hashlib.md5()
m.update(new_pwd.encode('utf-8'))
new_pwd = m.hexdigest()[0:16]
with open('username.txt', 'r', encoding='utf-8') as f:
for line in f:
real_name, real_pwd = line.split('|')
if username == real_name:
print('用户已存在')
break
else:
with open('username.txt', 'a+', encoding='utf-8')as f:
date = '%s|%s\n' % (username, new_pwd)
f.write(date)
print('注册成功')
break
while True:
username = input('登录')
password = input('请输入密码')
new_pwd = password + res
m = hashlib.md5()
m.update(new_pwd.encode('utf-8'))
new_pwd = m.hexdigest()[0:16]
with open('username.txt', 'r', encoding='utf-8')as f:
for line in f:
real_name, real_pwd = line.split('|')
if real_name == username and real_pwd.strip() == new_pwd:
print('成功')
break
else:
print('失败')
break