How can I remove and add record ( dictionary type) to a file. This is the program that I'm working on: the program should create a text file, print the contents of the text file, read the file after it's been created, add a record and print the contents of the file, remove a record(s) from the specified file, write it again, read it again, print the contens of the new file.
 Here is what I have so far:



import cPickle, shelve
def write_file():                                   
    CIT101 = ["Academic Computer Skills"]
    CIT111 = ["Database Management"]
    CIT115 = ["Intro to Computer scince"]
    CIT127 = ["ACCESS"]
    CIT211 = ! ["Systems Analysis and Design"]
    CIT216 = ["Visual Basic"]
    CIT218 = ["Intermediate Visual Basic"]
    CIT234 = ["Decision Support Using Excel"]
    pickle_file = open("pickles1.dat","w")

    cPickle.dump(CIT101, pickle_file)
    cPickle.dump(CIT111, pickle_file)
    cPickle.dump(CIT115, pickle_file)
    cPickle.dump(CIT127, pickle_file)
    cPickle.dump(CIT211, pickle_file)
    cPickle.dump(CIT216, pickle_file)
    cPickle.dump(CIT218, pickle_file)
    cPickle.dump(CIT234, pickle_file)
    print "A file has been created and the required specifications have been added"
    pickle_file.close



def read_file():
    pickle_file = open("pickles1.dat","r")
    CIT101 = cPickle.load(pickle_file)
    CIT111 = cPickle.load(pickle_file)
    CIT115 = cPickle.load(pickle_file)
    CIT127 = cPickle.load(pickle_file)
    CIT211 = cPickle.load(pickle_file)
    CIT216 = cPickle.load(pickle_file)
    CIT218 = cPickle.load(pickle_file)
    CIT234 = cPickle.load(pickle_file)
    pickle_file.close()


    pickles = shelve.open("pickles2.dat")
    pickles["CIT101"] = ["Academic Computer Skills"]
    pickles["CIT111"] = ["Database Management"]
    pickles["CIT115"] = ["Intro to Computer scince"]
    pickles["CIT127"] = ["ACCESS"]
    pickles["CIT211"] = ["Systems Analysis and Design"]
    pickles["CIT216"] = ["Visual Basic! "]
    pickles["CIT218"] = ["Intermediate Visual Basic"]
    pickles["CIT234"] = ["Decision Support Using Excel"]

    pickles.sync()

    for key in pickles.keys():
        print key, "-", pickles[key]

def dele_file():
    word_dele = raw_input("Which record do u want to delete?: ")

    if word_dele in picles.keys():
        del word_dele

    else:
        print "There is no such record in file pickles2.dat"
   
    pickles.close()


def add_record():
    CIT236 = ["SQL Programming"]
    CIT240 = ["Database programming"]
    pickle_file = open("pickles1.dat","a")

    cPickle.dump(CIT236, pickle_file)
    cPickle.dump(CIT240, pickle_file)
    print "New data was added to the file"
    pickle_file.close

   
def display_instructions():
    """Display the Main menue"""
    print \
          """
        Main Manue:
         
          1. Exit
          2. Create a new file and add specifications
          3. (not working)Add more courses to the file
          4. Read the file
          5. (not working)Delete f! ile

          """
   
# exit the program                               >>> 1 <<<
def over_program():
    """Exit the program"""
    print "Good Bye!"

   
def main():
    choice = None
    display_instructions()
   
   
    while choice != 1:
        choice = raw_input("\nChoice: ")
        if choice == "1":
            over_program()
            break
   
        elif choice == "2":
            write_file()

        elif choice == "3":
            add_to_file()
           
       
        elif choice == "4":
            read_file()

        elif choice == "5":
            delete_file()
       


        else:
            print "\nSorry, but", choice, "isn! 't a valid choice."



main()
raw_input("Press Enter Key to Exit.")   


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to