`
willzh
  • 浏览: 296788 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

13.4 shelve -- Python object persistence

阅读更多
import shelve

d = shelve.open(filename) # open -- file may get suffix added by low-level
                          # library

d[key] = data   # store data at key (overwrites old data if
                # using an existing key)
data = d[key]   # retrieve a COPY of data at key (raise KeyError if no
                # such key)
del d[key]      # delete data stored at key (raises KeyError
                # if no such key)
flag = d.has_key(key)   # true if the key exists
klist = d.keys() # a list of all existing keys (slow!)

# as d was opened WITHOUT writeback=True, beware:
d['xx'] = range(4)  # this works as expected, but...
d['xx'].append(5)   # *this doesn't!* -- d['xx'] is STILL range(4)!!!

# having opened d without writeback=True, you need to code carefully:
temp = d['xx']      # extracts the copy
temp.append(5)      # mutates the copy
d['xx'] = temp      # stores the copy right back, to persist it

# or, d=shelve.open(filename,writeback=True) would let you just code
# d['xx'].append(5) and have it work as expected, BUT it would also
# consume more memory and make the d.close() operation slower.

d.close()       # close it
分享到:
评论

相关推荐

    PHP Shelve-开源

    PHP Shelve,一种受python shelve模块启发的用于php对象的简单持久性机制。

    第五周-第09章节-Python3.5-内置模块详解之Shelve模块.avi

    第五周-第09章节-Python3.5-内置模块详解之Shelve模块.avi

    python3内置持久化模块shelve心得

    内置模块 shelve 可以将任意 Python 对象(pickle 模块能够处理的任何东西。)以类似字典的对象(shelf 对象)存在磁盘上以实现数据的持久保存。模块 shelve 生成的类似字典的对象 shelf 包含键 key 和 值 value 。...

    Shelve-Me:基于 RailsBackbone 的 GoodReads 克隆

    搁置我 一个读者可以相互联系并整理书籍的地方。 使用亚马逊的 API,用户可以通过输入 ISBN10 将图书添加到数据库中。 图书信息取自亚马逊服务器,书名、作者、描述、封面属性一键设置。 Sheve Me 使用 Backbone.js...

    Python3 shelve对象持久存储原理详解

    不需要关系数据库时,可以用shelve模块作为持久存储Python对象的一个简单的选择。类似于字典,shelf按键访问。值将被pickled并写至由dbm创建和管理的数据库。 1.1 创建一个新shelf 使用shelve最简单的方法就是利用...

    python-pickle-shelve

    python-pickle-shelve

    python3 shelve模块的详解

    主要介绍了python3 shelve模块的详解的相关资料,需要的朋友可以参考下

    Python数据持久化shelve模块用法分析

    主要介绍了Python数据持久化shelve模块用法,结合实例形式较为详细的总结分析了shelve模块的功能、原理及简单使用方法,需要的朋友可以参考下

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    python之shelve模块详解.docx

    **相同点:** 1.anydbm, shelve 都是对象持久化保存方法,将对象保存到文件里面,缺省的数据... anydbm的key和value的类型必须都是字符串,而shelve的key要求必须是字符串,value则可以是任意合法的python数据类型。

    学学Python_59标准模块7 shelve模块

    shelve模版,对应文件StandardLibrary9和databaseShelve,文章:https://blog.csdn.net/yysyangyangyangshan/article/details/85084999

    Python程序设计(第二版).chm

    Persistence Options in Python Section 16.3. DBM Files Section 16.4. Pickled Objects Section 16.5. Shelve Files Section 16.6. SQL Database Interfaces Section 16.7. PyForm: A Persistent ...

    Python Cookbook英文版

    8.4 Mutating Objects with shelve 8.5 Accesssing a MySQL Database 8.6 Storing a BLOB in a MySQL Database 8.7 Storing a BLOB in a PostgreSQL Database 8.8 Generating a Dictionary Mapping from ...

    Python shelve模块实现解析

    主要介绍了Python shelve模块实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    举例简单讲解Python中的数据存储模块shelve的用法

    主要介绍了举例简单讲解Python中的数据存储模块shelve的用法,shelveshelve模块与pickle模块的功能相近,比pickle用起来更为简单,需要的朋友可以参考下

    38-Shelve Instance 操作详解 1

    38-Shelve Instance 操作详解 1

    Python使用Shelve保存对象方法总结

    Shelve是一个功能强大的Python模块,用于对象持久性。搁置对象时,必须指定一个用于识别对象值的键。通过这种方式,搁置文件成为存储值的数据库,其中任何一个都可以随时访问。 Python中搁置的示例代码 要搁置对象,...

    shelve 用来持久化任意的Python对象实例代码

    shelve — 用来持久化任意的Python对象 这几天接触了Python中的shelve这个module,感觉比pickle用起来更简单一些,它也是一个用来持久化Python对象的简单工具。当我们写程序的时候如果不想用关系数据库那么重量级的...

    Python使用shelve模块实现简单数据存储的方法

    本文实例讲述了Python使用shelve模块实现简单数据存储的方法。分享给大家供大家参考。具体分析如下: Python的shelve模块提供了一种简单的数据存储方案,以dict(字典)的形式来操作数据。 #!/usr/bin/python ...

Global site tag (gtag.js) - Google Analytics