Python Day019:JSON、pickle、hashlib、hmac、shutil、shelve模块

## JSON模块

```python
json语音,是一种有语法规范的字符串,用来存放数据,完成各种语眼的数据交互。
1,就是{},[]的组合,{}存放双列信息(类比为字典),[]存放单列信息(类比为列表)。
2,{}中的key必须是""包裹的字符串。
3,{}的value与[]中支持的值的类型:int|float|str|dict|list|bool|null

序列化:将Python中的对象转换为字符串
dumps:将对象直接序列化成字符串
dump:将对象序列化成字符串存储到文件中
obj = {'name': 'Owen', "age": 18, 'height': 180, "gender": ""}
r1 = json.dumps(obj, ensure_ascii=False)  # 取消默认ascii编码,同该文件的编码 utf-8 py3默认,py2规定文件头
print(r1)
with open("1.txt","w",encoding="utf-8")as wr
    json.dump(obj,wf,ensure_ascii=False)
反序列化:
    json_str ='{"name": "Owen", "age": 18, "height": 180, "gender": "男"}'
    r2=json.loads(json_str,encoding="utf-8")#默认跟当前文件被解释器执行的编码走。
    print(r2,type(r2))
总结:将对象序列化成json语言时,对对象有什么要求?
将json语言反序列化时,对

```

## pickle

```python
为什么有很多序列化和反序列化模块
因为程序中出现的各种各样的对象需要持久化存储,必须要序列化。
序列化:对象转换成字符串
序列化目的:存、传

import pickle
obj = {"name": 'Owen', "age": 18, "height": 180, "gender": ""}
# 序列化
r1 = pickle.dumps(obj)
print(r1)
with open('2.txt', 'wb') as wf:
    pickle.dump(obj, wf)

# 反序列化
with open('2.txt', 'rb') as rf:
    data = rf.read()
    o1 = pickle.loads(data)
    print(o1, type(o1))

    rf.seek(0, 0)  # 游标移到开头出现读
    o2 = pickle.load(rf)
    print(o2, type(o2))
pickle将对象序列化成二进制字符串。所以,它可以序列化任意对象。

```

## hashlib

```python
特点:不可逆的加密方式md5
解密:只能碰撞解密
加密对象:用于传输数据(字符串类型)
为什么不需要解密:多用于密码验证,不需要解密获取。通过登录密码对后台保存的密码进行碰撞,匹配成功即可。
加密步骤:
    1,获取加密对象:lock_obj=hashlib.md5()
    2,添加加密数据:lock_obj.update(b"")
    3,获取加密结果:lock_obj.hexdigest()
lock = hashlib.md5(b'...')
lock.update(b'...')
# ...
lock.update(b'...')
res = lock.hexdigest()
print(res)
    
#加盐加密
    1,原数据过于简单,通过复杂的盐也可以提高解密难度
    2,即使碰撞成功,也不能识别有效数据与盐。
lock_obj = hashlib.md5()
lock_obj.update(b'qianyan')
lock_obj.update(b'123')
lock_obj.update(b'houyan')
res = lock_obj.hexdigest()
print(res)

# 了了解:其他算法加密
lock_obj = hashlib.sha3_256(b'1')
print(lock_obj.hexdigest())
lock_obj = hashlib.sha3_512(b'1')
print(lock_obj.hexdigest())


```

## hmac

```python
import hmac
# hmac.new(arg)  # 必须提供一个参数
cipher = hmac.new('加密的数据'.encode('utf-8'))
print(cipher.hexdigest())

cipher = hmac.new('前盐'.encode('utf-8'))
cipher.update('加密的数据'.encode('utf-8'))
print(cipher.hexdigest())

cipher = hmac.new('加密的数据'.encode('utf-8'))
cipher.update('后盐'.encode('utf-8'))
print(cipher.hexdigest())

cipher = hmac.new('前盐'.encode('utf-8'))
cipher.update('加密的数据'.encode('utf-8'))
cipher.update('后盐'.encode('utf-8'))
print(cipher.hexdigest())

```

## shutil

```python
#基于路径的文件复制:
shutil.copyfile("source_file","target_file")
#基于流的文件复制:
with open("source_file","rb") as r,('target_file', 'wb') as w:
    shutil.copyfileobj(r,w)
    
#递归删除目标目录
shutil.rmtree("target_folder")

#文件移动
shutil.move("oldfile","newfile")

#文件压缩
file_name:被压缩后形成的文件名  format:压缩的格式  archive_path:要被压缩的文件夹路径
shutil.make_archive('file_name', 'format', 'archive_path')
# 文件夹解压
unpack_file:被解压文件 unpack_name:解压后的名字 format解压格式
shutil.unpack_archive('unpack_file', 'unpack_name', 'format')

```

## shelve

```python
# 将序列化文件操作dump与load进行封装
shv_dic = shelve.open("target_file")  # 注:writeback允许序列化的可变类型,可以直接修改值
# 序列化:存
shv_dic['key1'] = 'value1'
shv_dic['key2'] = 'value2'

# 文件这样的释放
shv_dic.close()

shv_dic = shelve.open("target_file", writeback=True)
# 存 可变类型值
shv_dic['info'] = ['原数据']

# 取 可变类型值,并操作可变类型
# 将内容从文件中取出,在内存中添加, 如果操作文件有writeback=True,会将内存操作记录实时同步到文件
shv_dic['info'].append('新数据')

# 反序列化:取
print(shv_dic['info'])  # ['原数据', '新数据']

shv_dic.close()
```
原文地址:https://www.cnblogs.com/huhongpeng/p/10922751.html