Storing Objects in a Shelve File : shelve « Database « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » Database » shelve 
15.5.2.Storing Objects in a Shelve File
import shelve

flights = {"1":"A""2":"B""3":"C"}
times = ["230pm""320pm""420pm"]

db = shelve.open("shelved.dat""n")

db['flights'] = flights
db['times'] = times

print db.keys()

db.close()

f = open("shelved.dat""r")
data = f.read()
print data
f.close()

# Retrieving Objects from a Shelve File

db = shelve.open("shelved.dat""r")

for k in db.keys():
    obj = db[k]
    print "%s: %s" (k, obj)

flightDB = db['flights']
flights = flightDB.keys()
cities = flightDB.values()
times = db['times']

x = 0
for flight in flights:
    print ("Flight %s leaves for %s at %s" (flight, cities[x],  times[x]))
    x+=1

db.close()
15.5.shelve
15.5.1.object returned by shelve.open is not an ordinary mapping
15.5.2.Storing Objects in a Shelve File
15.5.3.Changing Objects in a Shelve File
15.5.4.Writing to shelve file.
15.5.5.Reading a shelve file.
15.5.6.database based on shelve file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.