•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,316 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,850 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 2247 | Replies: 15
![]() |
•
•
•
•
Since you want to save and later load an entire container object, the module pickle is what you want to use. Hre is a simple example:
In your case you would pickle the students dictionary!# pickle saves and loads any Python object intact # via a byte stream, use cpickle for higher speed import pickle myList1 = [12, 52, 62, 02, 3.140, "Monte"] print "Original list:" print myList1 file = open("list1.dat", "w") pickle.dump(myList1, file) file.close() file = open("list1.dat", "r") myList2 = pickle.load(file) file.close() print "List after pickle.dump() and pickle.load():" print myList2
slight issue, when I load the file, the students grades aren't preserved, just the maximum grades. This is the output from loading:
Grades 1 2 3 4 5
#Max 25 25 50 25 100
Not:
Charles 15 24 45 20 90
How do I get the students names and other data, or make sure that it saves them?
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation:
Rep Power: 10
Solved Threads: 176
•
•
•
•
What does your student dictionary look like just before you pickle.dump() it?
Also, you could write a small Python program that pickle.load() that file and looks at its contents.
#Max 25 25 50 25 100
Becky 9 20 30 16 57
Charles 15 20 50 25 98
Steve 25 14 45 24 19
That's what it looks like. This is what happens:
#Max 25 25 50 25 100
It just erases the students data when pickle dumps it.
Watch out when you start your program that your basic students dictionary does not override the newly loaded dictionary. Here is your code sample that works:
import pickle
max_points = [25,25,50,25,100]
assignments = ["hw ch 1","hw ch 2","quiz ","hw ch 3","test"]
try:
file = open("students.dat", "r")
students = pickle.load(file)
file.close()
except:
students = {"#Max":max_points}
print "students.dat file not found, starting with", students
def print_menu():
print "1. Add student"
print "2. Remove student"
print "3. Print grades"
print "4. Record grade"
print "5. Exit"
def print_all_grades():
print "\t",
for i in range(len(assignments)):
print assignments[i],"\t",
print
keys = students.keys()
keys.sort()
for x in keys:
print x,"\t",
grades = students[x]
print_grades(grades)
def print_grades(grades):
for i in range(len(grades)):
print grades[i],"\t\t",
print
print_menu()
menu_choice = 0
while menu_choice != 5:
print
menu_choice = input("Menu Choice (1-6): ")
if menu_choice == 1:
name = raw_input("Student to add: ")
students[name] = [0]*len(max_points)
elif menu_choice == 2:
name = raw_input("Student to remove: ")
if students.has_key(name):
del students[name]
else:
print "Student: ",name," not found"
elif menu_choice == 3:
print_all_grades()
elif menu_choice == 4:
print "Record Grade"
name = raw_input("Student: ")
if students.has_key(name):
grades = students[name]
print "Type the number of the grade to record"
print "Type a 0 (zero) to exit"
for i in range(len(assignments)):
print i+1," ",assignments[i],"\t",
print
print_grades(grades)
which = 1234
while which != -1:
which = input("Change which Grade")
which = which-1
if 0 <= which < len(grades):
grade = input("Grade: ")
grades[which] = grade
elif which != -1:
print "Invalid Grade Number"
else:
print "Student not found"
elif menu_choice != 5:
print_menu()
print students # test
# update students.dat file upon exit
file = open("students.dat", "w")
pickle.dump(students, file)
file.close() Last edited by Ene Uran : Sep 5th, 2006 at 12:28 pm.
drink her pretty
•
•
•
•
Watch out when you start your program that your basic students dictionary does not override the newly loaded dictionary. Here is your code sample that works:
import pickle max_points = [25,25,50,25,100] assignments = ["hw ch 1","hw ch 2","quiz ","hw ch 3","test"] try: file = open("students.dat", "r") students = pickle.load(file) file.close() except: students = {"#Max":max_points} print "students.dat file not found, starting with", students def print_menu(): print "1. Add student" print "2. Remove student" print "3. Print grades" print "4. Record grade" print "5. Exit" def print_all_grades(): print "\t", for i in range(len(assignments)): print assignments[i],"\t", print keys = students.keys() keys.sort() for x in keys: print x,"\t", grades = students[x] print_grades(grades) def print_grades(grades): for i in range(len(grades)): print grades[i],"\t\t", print print_menu() menu_choice = 0 while menu_choice != 5: print menu_choice = input("Menu Choice (1-6): ") if menu_choice == 1: name = raw_input("Student to add: ") students[name] = [0]*len(max_points) elif menu_choice == 2: name = raw_input("Student to remove: ") if students.has_key(name): del students[name] else: print "Student: ",name," not found" elif menu_choice == 3: print_all_grades() elif menu_choice == 4: print "Record Grade" name = raw_input("Student: ") if students.has_key(name): grades = students[name] print "Type the number of the grade to record" print "Type a 0 (zero) to exit" for i in range(len(assignments)): print i+1," ",assignments[i],"\t", print print_grades(grades) which = 1234 while which != -1: which = input("Change which Grade") which = which-1 if 0 <= which < len(grades): grade = input("Grade: ") grades[which] = grade elif which != -1: print "Invalid Grade Number" else: print "Student not found" elif menu_choice != 5: print_menu() print students # test # update students.dat file upon exit file = open("students.dat", "w") pickle.dump(students, file) file.close()
So this code will auto save it? Thanks. Now to the final chapter: Error checking!
. Good! The try/except is part of error checking you will tackle next.
What it does in the above code is the following, it tries to load the data file, if there is an error like a missing data file, it does the except part. In the except part you can be more specific about the exact error. Well anyway, have fun learning Python.
What it does in the above code is the following, it tries to load the data file, if there is an error like a missing data file, it does the except part. In the except part you can be more specific about the exact error. Well anyway, have fun learning Python.
drink her pretty
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: Trying to save the info in the student library.
- Next Thread: Graphics and Game commands:



Linear Mode