By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

How to read specific item in a list

P: 3
I have created a list and would like to read specific items in it, I only can read 1

Expand|Select|Wrap|Line Numbers
  1. import cPickle, sys, shelve
  2.  
  3. ## open file that stores scores
  4.  
  5. choice = None
  6. employees = []
  7. while choice != "0":
  8.  
  9.     print \
  10.             """
  11.             411 TUCKSHOP
  12.  
  13.             OPTIONS:
  14.             0 - Exit
  15.             1 - Employee: Add New Employee
  16.             2 - Employee: Display Balance
  17.             3 - Show All Employees and Balances
  18.             4 - Employee: Make deposit
  19.             5 - Purchase items @ Tuck Shop
  20.             6 - Remove all emmployees from database
  21.  
  22.             """
  23.     choice = raw_input("Choice: ")
  24.     print
  25.  
  26.     #exit if choice is "0"
  27.     if choice == "0":
  28.         print "Good bye, please call again..."
  29.  
  30.     #Choice 1 - Add Employees to a file    
  31.     elif choice == "1":
  32.         #Create a sequence
  33.         empnum = int(raw_input("Enter Employee Number: " ))
  34.         name = raw_input("Enter Employee Name:" )
  35.         sname = raw_input("Enter Employee Surname:" )
  36.         dept = raw_input("Enter Department:" )
  37.         bal = float(raw_input("Employee Balance R: "))
  38.         entry = (empnum, name, sname, dept, bal)
  39.         employees.append(entry)
  40.  
  41.         #create pickle file
  42.         pickle_file = open("emp_data.dat", "a+")
  43.         cPickle.dump(entry, pickle_file)
  44.         #close pickle file
  45.         pickle_file.close()
  46.  
  47.         #create a shelve
  48.         #empshelve = shelve.open("emp_data2.da")
  49.         #empshelve ["emp_number"] = [emp_number]
  50.  
  51.         #Ensure sync
  52.         #empshelve.sync()
  53.         #close shelve
  54.         #empshelve.close()
  55.  
  56.     #Choice 2 - Display Balances
  57.     elif choice == "2":
  58.  
  59.         pickle_file = open("emp_data.dat", "r")
  60.         emps = cPickle.load(pickle_file)
  61.         #Prompt user for search
  62.         for entry in employees:
  63.             empnum, name, sname, dept, bal = entry
  64.         print name, "\t", bal
  65.         empn = int(raw_input("\nChoose employee number: "))
  66.  
  67.         print "\nDisplaying Balance\n"
  68.         if empn in emps:
  69.             print "Balance ==> R ", emps[4] 
  70.         else:
  71.             print "Sorry"
  72.         pickle_file.close()
  73.  
Aug 18 '11 #1
Share this Question
Share on Google+
7 Replies


P: 10
In Python 3 the print statement is gone, it is replaced by the print function.
http://www.harshj.com/2008/12/09/the...n-in-python-3/
Aug 18 '11 #2

Expert 100+
P: 621
You have to check the first element of the tuple against the entry number:
Expand|Select|Wrap|Line Numbers
  1.         found = False
  2.         for entry in employees:
  3.             if empn == entry[0]:
  4.                 print "Balance ==> R ", entry[4]
  5.                 found = True
  6.         if not found:
  7.             print "Not a valid employee number" 
Generally, a dictionary is used, with the employee number as the key pointing to a list of items http://greenteapress.com/thinkpython/html/book012.html so you can then use, if empn in employee_dict.
Aug 18 '11 #3

Expert 100+
P: 621
A couple of hints:
# 6 is easy --> employees = [] ## re-initialize as an empty list
When adding an employee, check if the number is already in the list, since you don't want the same employee number in the list twice.
Use a function to look up an employee number, since you will be doing this for every choice on the menu.
Use a list instead of a tuple so you can change it (make deposits)
Read the pickle file once, before the while loop, instead of every time through the loop, and write/dump/close it once, after the while loop. Adds will be appended to the list that will be pickled.
Aug 18 '11 #4

P: 3
All your suggestions have been invaluable and I have gotten someway but when I cPickle the file I fail to read the contents back as a list
Expand|Select|Wrap|Line Numbers
  1. #Choice 2 - Display Balances
  2.     elif choice == "2":
  3.  
  4.         lists = []
  5.         infile = open('emp_data.dat', 'r')
  6.         #inlist = cPickle.load(infile)
  7.         while 1:
  8.             try:
  9.                 lists.append(cPickle.load(infile))
  10.  
  11.             except (EOFError):
  12.                 break
  13.         infile.close()
  14.         #Prompt user for search
  15.         print len(lists)
  16.         empn = int(raw_input("\nChoose employee number: "))
  17.         found = False 
  18.         for entry in lists: 
  19.            if empn == entry[0]: 
  20.                 print "Balance ==> R ", entry[4] 
  21.                 found = True 
  22.         if not found: 
  23.             print "Not a valid employee number"
Aug 26 '11 #5

Expert 100+
P: 621
It should be
lists = cPickle.load(infile)
http://wiki.python.org/moin/UsingPickle
Aug 26 '11 #6

P: 3
hi dwblas;

I'm really am loosing my mind now. my code is only able to display the first entry, from there my if statement only evaluates to false for all other entries. please correct my code:

#Choice 2 - Display Balances
elif choice == "2":

lists = []
infile = open('emp_data.dat', 'r')
lists = cPickle.load(infile)

empn = int(raw_input("\nChoose employee number: "))
found = False
for entry in lists:
if empn == entry[0]:
print "Balance ==> R ", entry[4]
found = True
if not found:
print "Not a valid employee number"
infile.close()
Aug 28 '11 #7

Expert 100+
P: 621
The pickle statement is still wrong. The link in my previous post shows an example of pickling and unpickling. If you are not going to read the post then there is little reason to post. Also, for future problems add a print statement. In this case:
Expand|Select|Wrap|Line Numbers
  1. for entry in lists:
  2.     print "testing" empn, entry[0]
  3.     if empn == entry[0]: 
please correct my code
That's just rude.
Aug 28 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.