juergenkemeter
Programmer
Hi!
I get an AttributeError when using SPE with the two scripts below. When using IDLE, this message doesnt occur.
First Script reads in data and writes it into a shelve file:
I get an AttributeError when using SPE with the two scripts below. When using IDLE, this message doesnt occur.
First Script reads in data and writes it into a shelve file:
Code:
# Writing to shelve file.
import sys
import shelve
# open shelve file
try:
outCredit = shelve.open( "credit.dat" )
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit( 1 )
print "Enter account number (1 to 100, 0 to end input)"
# get account information
while 1:
# get account information
accountNumber = int( raw_input(
"\nEnter account number\n? " ) )
if 0 < accountNumber <= 100:
print "Enter lastname, firstname, balance"
currentData = raw_input( "? " )
outCredit[ str( accountNumber ) ] = currentData.split()
elif accountNumber == 0:
break
outCredit.close() # close shelve file
[\CODE]
-------------------------------------------------
In the Shell, I entered e.g.
23
Barker Doug 23.44
55
Horst Huber 55
0
--------------------------------------------------
When trying to print out the data with the following script, this error message is displayed in the SPE IDE:
AttributeError: 'int' object has no attribute 'keys'
here is the script for printing out the stored data in teh Credit.dat - file:
[CODE]
# Fig. 14.9: fig14_09.py
# Reading a shelve file.
import sys
import shelve
# print formatted credit data
def outputLine( account, aList ):
print account.ljust( 10 ),
print aList[ 0 ].ljust( 10 ),
print aList[ 1 ].ljust( 10 ),
print aList[ 2 ].rjust( 10 )
# open shelve file
try:
creditFile = shelve.open( "credit.dat" )
except IOError:
print >> sys.stderr, "File could not be opened"
sys.exit( 1 )
print "Account".ljust( 10 ),
print "Last Name".ljust( 10 ),
print "First Name".ljust( 10 ),
print "Balance".rjust( 10 )
# display each account
for accountNumber in creditFile.keys():
outputLine( accountNumber, creditFile[ accountNumber ] )
creditFile.close() # close shelve file
[\CODE]