shelve是一个简单的数据存储方案,类似key-value数据库,可以很方便的保存python对象,其内部是通过pickle协议来实现数据序列化。...shelve只有一个open()函数,这个函数用于打开指定的文件(一个持久的字典),然后返回一个shelf对象。shelf是一种持久的、类似字典的对象。...db['hobby'] = ['篮球', '看电影', '弹吉他'] db['other_info'] = {'sno': 1, 'addr': 'xxxx'} # 读取数据 with shelve.open...("stu.db") as db: db['Tom'] = tom db['Jerry'] = jerry # 读取数据 with shelve.open("stu.db") as db...: print(db['Tom']) print(db['Jerry']) import shelve f = shelve.open("shelve_test.db") f.update
shelve类似于一个存储持久化对象的持久化字典,即字典文件。 使用方法也类似于字典。 保存对象至shelve文件中: #!...d = shelve.open('shelve_test') #基本信息 info = { 'age':22, "job":"it" } #姓名 name = ["jack", "rain...、shelve_test.dat、shelve_test.dir 从文件中读取对象: #!.../usr/bin/env python # coding: utf-8 __author__ = 'www.py3study.com' import shelve # 打开一个文件 d = shelve.open...# 打开一个文件 d = shelve.open('shelve_test') #从文件中读取之前存储的对象 info_new = d.get("info") #直接对对象进行修改 info_new
python使用shelve保存变量 1、用shelve模块,可以将Python中的变量保存到二进制的shelf文件中。 这样,程序就可以从硬盘中恢复变量的数据。...import shelve shelfFile = shelve.open('mydata') cats = ['Zonphie','Pooka','Simon'] shelfFile['cats'] ...shelfFile = shelve.open('mydata') type(shelfFile) shelve.DbfilenameShelf shelfFile['cats'] shelfFile.close...() 以上就是python使用shelve保存变量的方法,希望对大家有所帮助。
image.png pickle image.png image.png shelve image.png ZODB
Python标准库shelve提供了二进制文件操作的功能,可以像字典一样赋值即可写入文件,也可以像字典一样读取二进制文件,有点类似于NoSQL数据库MongoDB的操作。...>>> import shelve #导入shelve模块 >>> fp = shelve.open('shelve_test.dat')...#写入文件内容 >>> fp.close() #关闭文件 >>> fp = shelve.open...('shelve_test.dat') >>> print(fp['zhangsan']['age']) #查看文件内容 38 >>> print(fp['lisi']['qq'
''' python中的shelve模块,可以提供一些简单的数据操作 他和python中的dbm很相似。...区别如下: 都是以键值对的形式保存数据,不过在shelve模块中, key必须为字符串,而值可以是python所支持的数据 类型。...对象 sh = shelve.open('c:\\test\\hongten.dat', 'c') 删除shelve对象中的某个键值对 del sh['d'] 遍历所有数据...11 ''' 12 python中的shelve模块,可以提供一些简单的数据操作 13 他和python中的dbm很相似。...对象 28 sh = shelve.open('c:\\test\\hongten.dat', 'c') 29 30 删除shelve对象中的某个键值对 31 del
test','wb') as f: 5 pickle.dump(list,f) 6 7 with open('test','rb') as f2: 8 pickle.load(f2) shelve...shelve也是python提供给我们的序列化工具,比pickle用起来更简单一些。...shelve只提供给我们一个open方法,是用key来访问的,使用起来和字典类似。...1 import shelve 2 f = shelve.open('test1') 3 f['key'] = {'a':1, 'b':2, 'c':'sss'} #直接对文件句柄操作,就可以存入数据...4 f['key2'] = {'d':3, 'e':4, 'f':'ddd'} 5 f.close() 6 7 f1 = shelve.open('test1') 8 dic1 = f1[
如下操作: import shelve f_shelve = shelve.open('shelve') # 创建一个文件句柄 f_shelve["name"] = "小明" # 向文件中存放数据...f_shelve["age"] = 21 # 向文件中存放数据 f_shelve["sex"] = "男" 运行后会生成3个文件:shelve.bak shelve.dat shelve.dir shelve.dat...import shelve f_shelve = shelve.open('shelve') # 创建一个文件句柄 # 打印文件内容,和类型 print(f_shelve["name"],f_shelve...import shelve f_shelve = shelve.open('shelve') # 创建一个文件句柄 f_shelve["list"] = [1,2,3] # 向文件中添加数据列表...import shelve # 创建一个特殊的文件句柄,并添加回写功能 f_shelve = shelve.open('shelve',writeback=True) # 启用会写 f_shelve
直到发现了shelve,它完美解决了我的困扰 - 简单又强大,关键是开箱即用!...安装和配置作为Python标准库,shelve无需安装,直接导入即可:import shelve不过要注意,在Windows系统上使用shelve时,会生成三个文件:.dat、.dir和.bak。...建议在程序中指定文件名时不要加扩展名,让shelve自己处理。...基本用法shelve最大的特点就是可以像使用字典一样操作它:# 创建/打开一个shelve数据库with shelve.open('mydata') as db: # 存储各种Python对象...进阶技巧shelve还有一些鲜为人知的高级特性:with shelve.open('mydata', writeback=True) as db: # 启用writeback可以自动同步可变对象的修改
实际上,在 Python 中,我们可以使用shelve模块,像读写字典一样持久化存储数据。...例如,在 write.py文件中,我们写如下代码: import shelve with shelve.open('data') as db: db['username'] = 12345678...但shelve模块没有这个限制,所有能被 pickle的对象,都可以存入,例如: import shelve with shelve.open('data') as db: db['complex_data...'] = [{'a': 1, 'b': [1, 2, 3]}, 2, 'a'] 需要注意的是,shelve模块底层基于pickle模块,所以当别人传给你一个 shelve生成的文件时,不能贸然打开,否则可能会执行危险的代码...另外,shelve模块只支持多线程同时读取。不支持多线程写入,也不支持同时读写。 关于shelve的更多参数,可以参阅它的官方文档[1].
持久化工具 类似字典,用kv对保存数据,存取方式跟字典也类似 open,close # 使用shelve创建文件并使用 import shelve # 打开文件 # shv相当于一个字典 shv =...shelve.open(r"shv.db") shv['one'] = 1 shv['two'] = 2 shv['three'] = 3 shv.close() # 通过以上案例发现,shelve...自动创建的不仅仅是一个shv.db 文件,还包括其他格式文件 # shelve 读取案例 shv = shelve.open(r'shv.db') try: print(shv['one'...# shelve 之只读打开 import shelve shv = shelve.open(r'shv.db', flag='r') try: k1 = shv['one'] print...(k1) finally: shv.close() 1 import shelve shv = shelve.open(r'shv.db') try: shv['one'] = {"
模块 shelve 模块也用于序列化,shelve 模块是在 pickle 模块上做了一层封装,也仅支持两个Python程序之间进行交换~,优点是 shelve 模块 可以序列化 Python 的所有数据类型...shelve 模块存取过程: import shelve class Person: def __init__(self, name, age): self.name = name...使用 shelve 模块序列化之后存放到文件中,然后取出(get)对可变对象进行更改,这个时候,已经改变的可变对象只是保存在内存中,不会被写入到文件中,看如下示例: import shelve f =...')) # 输出结果: [1, 2, 3] 若要进行更改需要重新写入,即重新序列化: import shelve f = shelve.open(r'/tmp/test_shelve') f['lst_info...打开文件时,设置 writeback 为True: f = shelve.open(r'/tmp/test_shelve', writeback=True) f['lst_info'] = [1, 2
shelve shelve 的使用方式和 dbm 几乎是一致的,区别就是 shelve 的序列化能力要更强,当然速度自然也就慢一些。...import shelve # 第二个参数表示模式,默认是 c # 因此文件不存在会创建,存在则追加 sh = shelve.open("shelve") sh["name"] = ["S 老师",...import shelve sh = shelve.open("shelve") print(sh["name"]) print(sh["name"][2] == "电烤架") """ ['S 老师...import shelve sh = shelve.open("shelve") # 需要注意的是,People 是我们自己定义的类 # 如果你想要将其还原出来,那么该类必须要出现在当前的命名空间中...import shelve # 打开文件,设置键值对 sh = shelve.open("shelve") sh["name"] = "古明地觉" sh["score"] = [80, 80, 80]
python为开发者提供了一个轻量级的数据存储方式shelve,对于一些轻量数据,使用shelve是个比较不错的方式。对于shelve,可以看成是一个字典,它将数据以文件的形式存在本地。...1 import shelve 2 3 #shelve提供一个open方法,接受一个文件作为参数。...4 f=shelve.open('ceshi.text') #拿到一个shelve句柄,同时给shelve传入一个文件作为参数 5 6 #写入信息 7 f['info'] = {'name':...27 f = shelve.open('test.txt') 28 f['list'] = [1,2,3] #保存列表 29 lis = f['list'] 30 print...(lis) 31 32 33 34 35 36 #关于获取shelve数据内容,shelve提供了一个get方法。
""" Implement a GUI for viewing and updating class instances stored in a shelve; the shelve lives on...1 or more local files; """ from tkinter import * from tkinter.messagebox import showerror import shelve...shelvename = 'class-shelve' fieldnames = ('name', 'age', 'job', 'pay') class Person: def init(self...self.pay *= (1.0 + percent) def makeWidgets(): global entries window = Tk() window.title('People Shelve...for field in fieldnames: setattr(record, field, eval(entries[field].get())) db[key] = record db = shelve.open
serialization,是将对象转化为二进制码存入文件中,主要函数pickle.dump(obj,file),pickle.load(file) *在每个文件加入后缀.pkl,实现逐行数据存入 *使用shelve...filename, '=>\n ', record) suefile = open('sue.pkl', 'rb') print(pickle.load(suefile)['name']) import shelve... db = shelve.open('people-shelve') db['bob'] = bob db['sue'] = sue db.close() db = shelve.open('people-shelve...') sue=db['sue'] sue['name']='Susie Q' #不能直接赋值给db['sue']['name'] db['sue']=sue db.close db = shelve.open...('people-shelve') print db db.close()
Shelve 如果说 Python 中的字典(dict)是保存在内存中的,那么标准库 shelve[1] 就像是保存在文件中的字典,它的值可以为为任意 pickle 模块能够处理的 Python 对象,...使用方法:shelve.open(filename, flag='c', protocol=None, writeback=False) 示例代码如下,可以看下注释: import shelve #...store.db # 推荐这种写法 with shelve.open("store") as db: db["data"] = {"name": "Python七号", "author":..."somenzz"} #读取 with shelve.open("store") as db: print(db["data"]) #out {'name': 'Python七号', '...独学而无友,则孤陋而寡闻 参考资料 [1] shelve: https://docs.python.org/zh-cn/3/library/shelve.html [2] pickleDB: https
1、G.next() 2、pickle 用法 3、shelve 4、继承类的初始化写法:Obj.init 5 thinker from tkinter import * Label...cgi:::::: """ Implement a web-based interface for viewing and updating class instances stored in a shelve...; the shelve lives on server (same machine if localhost) """ import cgi, shelve, sys, os...# cgi.test() dumps inputs shelvename = 'class-shelve' # shelve files are in cwd fieldnames...form[field].value)) db[key] = record fields = record.dict fields['key'] = key return fields db = shelve.open
将 Python 程序中的变量保存到二进制的 shelf 文件中会用到shelve 模块。shelve 模块让你在程序中添加“保存” 和“打开” 功能,方便程序下一次运行时加载变量。...python3 # mcb.pyw - 程序的名称,用来保存和加载多重剪切板 # 导入用到的模块 import shelve, pyperclip, sys # 初始化 shelf 文件 mcb. mcbShelf...= shelve.open('mcb') # 获取命令行参数 command = sys.argv[1].lower() # TODO: 保存剪切板内容,并为每次复制的内容设置一个关键字. # TODO...删除所有关键字,清空剪切板 elif command == 'delete_all': # 清空 shelf 文件 mcbShelf.clear() # mcbShelf = shelve.open...完整程序 import shelve import pyperclip import sys mcbShelf = shelve.open('mcb') command = sys.argv[1].lower
10 持久化 pickle import pickle f=open("a.txt","wb") pickle.dump(obj,f) obj=pickle.load(f) 11 持久化shelve...import shelve db=shelve.open("a.txt") db["key1"]=obj db.close()
领取专属 10元无门槛券
手把手带您无忧上云