#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2009
    Posts
    18
    Rep Power
    0

    Pickle / File Read Issue


    I have used the two below functions in my program. If I run the writing one a few times and then run the read one, it still only shows the first entry that was input there. What is wrong with the code such that it won't show me all he entries added to the file?

    Note: "entry" is a dictionary entry, of the form
    [date, str, str, int, int, str]

    writing function
    Code:
    openfile = open("results.pkl","ab")
    pkloutput = pickle.Pickler(openfile)
    pkloutput.dump(entry)
    openfile.close()
    reading function
    Code:
    openfile = open("results.pkl","rb")
    pkloutput = pickle.Unpickler(openfile)
    data = pkloutput.load()
    openfile.close()
    print (data)
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Posts
    5,179
    Rep Power
    483
    Looks like pickle can store only one object. You could make that object compicated to store a lot of information at once. You could use your pickle append method and retain the offsets of each object then recover them with seek before unpickling. That's clunky!

    I suggest instead that you look at the shelve module.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo