Python标准库之shelve模块(序列化与反序列化)

shelve模块是一个简单的key,value将内存数据通过文件持久化的模块,可以持久化任何picklel可支持的Python数据格式。

序列化

序列化源代码:

import shelve
import os

f = shelve.open("shelve_log")

d = {'1':'a','2':'b'}

def test():
	return os.system("calc")

f['dict'] = d
f['func'] = test
f.close()

  

运行后会在当前目录下生成后缀为bak、dat、dir文件。

打开后三份文件都是一样的,内容如下:

反序列化

反序列化读取:

import shelve
import os

f = shelve.open("shelve_log") 

d = {'1':'a','2':'b'} #这行不可少
def test():   #这行不可少
	return os.system("calc") #这行不可少

print(f.get('dict'))
f.get('func')()

  

猜你喜欢

转载自www.cnblogs.com/endust/p/12311888.html