tags:

views:

45

answers:

1

Hi, I have a file in which I have dumped a huge number of lists.Now I want to load this file into memory and use the data inside it.I tried to load my file using the "load" method of "pickle", However, for some reason it just gives me the first item in the file. actually I noticed that it only load the my first list into memory and If I want to load my whole file(a number of lists) then I have to iterate over my file and use "pickle.load(filename)" in each of the iterations i take. The problem is that I don't know how to actually implement it with a loop(for or while), because I don't know when I reach the end of my file. an example would help me a lot. thanks

+1  A: 

How about this:

lists = []
infile = open('yourfilename.pickle', 'r')
while 1:
    try:
        lists.append(pickle.load(infile))
    except (EOFError, UnpicklingError):
        break
infile.close()
eumiro
You should __never__ use `except:`; always specify the exception you are trying to catch (e.g. `except PicklingError:`). Also, the Python idiom for infinite loops is `while 1`, though that's nitpicky =).
katrielalex
@katrielalex - you're right, I fixed it. @Hossein - I have now tested possible errors, so please if it raises any other error than EOFError and UnpicklingError, add it to the list.
eumiro