Python模块-shelve模块

shelve模块也是用来序列化的,可以持久化任何pickle可支持的python数据格式,比pickle好用,也是python专属,可以dump多次数据,也可以直接修改数据

序列化

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import shelve

f = shelve.open('shelve_test')

names = ["John", "Jack", "Jane"]
info = {'name':'John','age':22}

f['name'] = names
f['infos'] = info

f.close()

生成了三个文件

反序列化

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"

import shelve

f = shelve.open('shelve_test')

#获取数据
print(list(f.keys())) #获取key名
print(f.get('name')) #获取key的值
print(f['infos']) #获取key的值
print(list(f.items())) #获取全部数据

#修改数据
f['name'] = ['a','b','c']
print(f['name'])

del f['infos'] #删除数据

f.close()

运行结果



posted @ 2018-02-15 01:05  Sch01aR#  阅读(211)  评论(0编辑  收藏  举报