常用模块:shelve模块

概述

通过学习shelve模块,感觉日后必能使用,留次痕迹,以备后查。shelve对pikle进行了封装,python语言中独有的,允许load和dump多次,是一种简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pikle的可支持的数据类型。

序列化

1 import shelve
2 names=["yan","alex","shanshan"]
3 info={"first_name":"yan","second_name":"long"}
4 with shelve.open("shelve_test") as f:
5     f['names']=names#将数据序列化到文件里
6     f["info"]=info
7     

输出:

反序列化

1 import shelve
2 names=["yan","alex","shanshan"]
3 info={"first_name":"yan","second_name":"long"}
4 with shelve.open("shelve_test") as f:
5     # f['names']=names
6     # f["info"]=info
7     print(f["names"])
8     # del f["info"]#可以删除
9     print(f["info"])

输出:

猜你喜欢

转载自www.cnblogs.com/yan-long/p/9450490.html