Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attribute Error using SPE IDE (Stani's Python Editor)

Status
Not open for further replies.

juergenkemeter

Programmer
Oct 6, 2004
27
NZ
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:

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]
 
the scripts are ok when running in IDLE. The error only occurs in SPE.
Juergen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top