Shelve作用:以键值对的形式保存数据
Shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写:key必须是字符串,而值可以是Python所支持的数据类型。
d[]等价于d.get()
# -*-coding:utf-8 -*-
__author__ = ‘xiaojiaxin‘
__file_name__ = ‘shelve‘
d={"name":"jiaxin","sex":"female"}
print(d["name"])
print(d.get("name"))
# jiaxin
# jiaxin
# 两个取值法都可以
shelve模块
import shelve
f=shelve.open(r"shelve_text")
#f["info"]={"name":"jiaxin","sex":"female"}
#创建数据后注释掉进行操作
data=f.get("info")
print(data)
# {‘name‘: ‘jiaxin‘, ‘sex‘: ‘female‘}
print(f.get("age","18"))
# 18
for i in f:
print(f[i])
# {‘name‘: ‘jiaxin‘, ‘sex‘: ‘female‘}
f.close()
原文地址:http://blog.51cto.com/10777193/2093387