shelve.open [python]

The shelve module implements persistent storage for arbitrary Python objects which can be pickled, using a dictionary-like API. Python的数据持久化操作主要有六类:普通文件、DBM文件、Pickled对象存储、shelve对象存储、对象数据库存储、关系数据库存储。

shelve模块是anydbm的增强版,它支持在”dict-like”对象中存储任何可以被pickle序列化的对象,但key也必须是字符串

import shelve, sys

def printshelf (shelf, stream=sys.stdout):  # print the entries in a shelf
    for k in shelf.keys():
        print >>stream, k, '=', shelf[k]

def clearshelf (shelf):                     # remove all keys in the shelf
    for k in shelf.keys():
        del shelf[k]

# create shelf
shelf = shelve.open("test.shelf")
clearshelf(shelf)
shelf["x"] = [1,2,3,4]
shelf["y"] = {'a':1, 'b':2, 'c':3}
printshelf(shelf)
shelf.close()

shelve.open(filenameflag=’c’protocol=Nonewriteback=False)

Open a persistent dictionary.

The filename specified is the base filename for the underlying database. As a side-effect, an extension may be added to the filename and more than one file may be created.

By default, the underlying database file is opened for reading and writing. The optional flag parameter has the same interpretation as the flag parameter of anydbm.open().

Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing

idb = shelve.open(‘idbpath’,flag=’r’)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: