文件数据操作模块 json 、 pickle、 shelve和configparser

1、序列化和反序列化的两个模块json和pickle,他们都有dump、dumps、load、loads方法

 1>dump方法,把python的数据格式转换成str或者Bytes(称为序列化),并存放到文件


文件数据操作模块 json 、 pickle、 shelve和configparser

 写入文件后的效果


文件数据操作模块 json 、 pickle、 shelve和configparser

 2>load方法,读取文件并且转成python的数据格式(反序列化),参数为打开文件的句柄,也叫文件对象

文件数据操作模块 json 、 pickle、 shelve和configparser

3>dumps和loads方法(只操作数据,不写入文件)

文件数据操作模块 json 、 pickle、 shelve和configparser

文件数据操作模块 json 、 pickle、 shelve和configparser

2、json于pickle的区别


 1、一个是写成str格式,一个是写成Bytes格式
 2、json只支持 str、int、list、tuple、dict;pickle支持python所有的数据类型
 3、json跨平台,他支持的序列化数据类型其他大部分语言也支持,pickle是python专用
文件数据操作模块 json 、 pickle、 shelve和configparser
3、shelve
pickle的升级版 shelve
shelve类似于一个key-value数据库,使用者可以将一个用户自定义的类实例保存到shelve中,下次直接取出来,就是一个Python内存对象
他只有一个函数就是open(),这个函数接收一个参数就是文件名,就调用close函数来关闭
 1>存储
文件数据操作模块 json 、 pickle、 shelve和configparser
2>读取
文件数据操作模块 json 、 pickle、 shelve和configparser

4、configparser(操作配置文件)

 1>读取

文件数据操作模块 json 、 pickle、 shelve和configparser

文件数据操作模块 json 、 pickle、 shelve和configparser

 1.2 单个取值,get方法

文件数据操作模块 json 、 pickle、 shelve和configparser

也可以直接conf['section']['option']这样取数据

2>写入

文件数据操作模块 json 、 pickle、 shelve和configparser

 文件数据操作模块 json 、 pickle、 shelve和configparser

 3>其他的增删方法诸如,conf.set('section', 'opition') , conf.remove_opition('section', 'opition')