«апис у файл

 

importsys
importshelve

try:
outCredit = shelve.open( "credit.dat" )
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit( 1 )

outCredit[ "2" ] = "data"

outCredit.close() # close shelve file

 

 

„итанн€ ≥з файлу

 

importsys
importshelve

def outputLine( account, aList ):
print account.ljust( 10 ),
print aList[ 0 ].ljust( 10 ),
print aList[ 1 ].ljust( 10 ),
print aList[ 2 ].rjust( 10 )

try:
creditFile = shelve.open( "credit.dat" )
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit( 1 )

foraccountNumber in creditFile.keys():
outputLine( accountNumber, creditFile[ accountNumber ] )

creditFile.close()

 

Ѕаза даних на Shelve

importsys, shelve

def store_person(db):
pid = raw_input('Enter unique ID number: ')
person = {}
person['name'] = raw_input('Enter name: ')
person['age'] = raw_input('Enter age: ')
person['phone'] = raw_input('Enter phone number: ')

db[pid] = person

def lookup_person(db):
pid = raw_input('Enter ID number: ')
field = raw_input('What would you like to know? (name, age, phone) ')
field = field.strip().lower()
print field.capitalize() + ':', db[pid][field]

def enter_command():
cmd = raw_input('Enter command (? forhelp): ')
cmd = cmd.strip().lower()
returncmd

database = shelve.open('c:\\database.dat')
store_person(database)
lookup_person(database)
database.close()