python学习之老男孩python全栈第九期_day020知识点总结——序列化模块、模块的导入和使用

一. 序列化模块

# 序列化 --> 将原本的字典、列表等内容转换成一个字符串的过程就叫做序列化
# 反序列化 --> 从字符串转换成数据类型的过程叫做反序列化
# 序列 -- 字符串

# 什么地方需要转换成字符串?
# 数据存储:写文件等
# 网络上传输的时候只能传bytes,故需要先转成字符串

# json -- 最重要:*****(5星级)
# pickle -- ****
# shelve -- ***

# json
# 好处:通用的序列化格式
# 弊端:只有很少的一部分数据类型能够通过json转化成字符串

# pickle
# 优点:所有的python中的数据类型都可以转化成字符串形式
# 弊端:pickle序列化的内容只有python能理解;且部分反序列化依赖代码

# shelve
# 序列化句柄
# 使用句柄直接操作,非常方便


# json:dumps 序列化方法;loads 反序列化方法
# import json
# dic = {'k1':'v1'}
# print(dic,type(dic)) # {'k1': 'v1'} <class 'dict'>
# str_dic = json.dumps(dic) # 序列化
# print(str_dic,type(str_dic)) # {"k1": "v1"} <class 'str'> 转换成字符串之后,里面的单引号都变成了双引号
#
# dic_dic = json.loads(str_dic) # 反序列化
# print(dic_dic,type(dic_dic)) # {'k1': 'v1'} <class 'dict'>

# 数字,字符串,列表,字典 一般可以序列化 ;集合不能转
# 元组也可以序列化,元组序列化是把元组转换成一个列表,再进行序列化;反序列化回来会转换成列表,而不是元组
# tu = (1,2,3,4)
# tu_str = json.dumps(tu)
# print(tu_str,type(tu_str)) # [1, 2, 3, 4] <class 'str'>
# tu_list = json.loads(tu_str)
# print(tu_list,type(tu_list)) # [1, 2, 3, 4] <class 'list'>

# json :dump:把数据类型转换成字符串,再传进文件;load
# dic = {'k1':'v1'}
# with open('fff','w',encoding='utf-8') as f:
# json.dump(dic,f)
# with open('fff',encoding='utf-8') as f:
# ret = json.load(f)
# print(ret,type(ret)) # {'k1': 'v1'} <class 'dict'>

# dic = {'k1':'中国'}
# with open('fff','w',encoding='utf-8') as f:
# json.dump(dic,f,ensure_ascii=False)
# with open('fff',encoding='utf-8') as f:
# ret = json.load(f)
# print(ret,type(ret))


# dic = {'k1':'中国'}
# with open('fff','w',encoding='utf-8') as f:
# json.dump(dic,f,ensure_ascii=False)
# json.dump(dic,f,ensure_ascii=False)
# with open('fff',encoding='utf-8') as f:
# ret1 = json.load(f)
# # ret2 = json.load(f) # 报错,只能一次读完
# print(ret1,type(ret1))
# # print(ret2,type(ret2))


# json
# dumps {} --> '{} '
# 要想一行一行的读
# '{} '
# '{}' loads

import json
# li = [
# {'k1':'111'},
# {'k2':'222'},
# {'k3':'333'}
# ]
# with open('file','w',encoding='utf-8') as f:
# for dic in li:
# str_dic = json.dumps(dic)
# f.write(str_dic+' ')
# l = []
# with open('file',encoding='utf-8') as f:
# for line in f:
# dic = json.loads(line.strip())
# l.append(dic)
# print(l)



# pickle和json用法基本一致
# pickle可以分步写和读,而json只能分步写



# shelve

import shelve
f = shelve.open('shelve_file')
f['key'] = {'int':10, 'float':9.5, 'string':'Sample data'} #直接对文件句柄操作,就可以存入数据
f.close()

import shelve
f1 = shelve.open('shelve_file')
existing = f1['key'] #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错
f1.close()
print(existing)




import shelve
f1 = shelve.open('shelve_file')
print(f1['key'])
f1['key']['new_value'] = 'this was not here before'
f1.close()

f2 = shelve.open('shelve_file', writeback=True)
print(f2['key'])
f2['key']['new_value'] = 'this was not here before'
f2.close()

二. 模块的导入和使用

# 文件
# import demo
# import demo
# import demo
# import demo # 同一个模块,即便写了多次,但依然只导入一次
# money = 2
# demo.read()
# print(demo.money)
# def read():
# print('my read func')
# # 找到模块
# # 创建这个模块的命名空间
# # 把文件中的名字都放到命名空间里
#
# import sys
# print(sys.modules.keys())
# # 先从sys.modules里查看是否已经被导入
# # 若没有被导入,就依据sys.path路径去寻找模块
# # 找到了就导入
# # 创建这个模块的命名空间
# # 执行文件,把文件中的名字都放到命名空间里
# print(sys.path)


import time as t
print(t.time())

# 重命名之后,只能用重命名之后的


# 导入模块顺序:
# 先导内置的
# 再导扩展的:django
# 再导自定义的


# 模块不会重复被导入: sys.moudles
# 从哪导入模块? sys.path


# import 模块名
# 模块名.变量名 和本文件中的变量名完全不冲突

# import 模块名 as 重命名的模块名:提高代码的兼容性
# import 模块1,模块2

# from import
# from 模块名 import 变量名
# 直接使用 变量名 就可以完成操作
# 如果本文件中有相同的变量名,会发生冲突

# from 模块名 import 变量名 as 重命名变量名
# from 模块名 import 变量名1,变量名2
# from 模块名 inport *
# 将模块中的所有变量名都放到内存中
# 如果本文件中有相同的变量名会发生冲突
# from 模块名import * 和 __all__ 是一对
# 如果没有没有这个变量,就会导入所有的名字
# 如果有 __all__ ,只导入 all 列表中的名字


# 包
# 一大堆模块的集合
原文地址:https://www.cnblogs.com/lpgit/p/9381128.html