Monday 23 November 2015

Using Pickle in Python

If you want to run a Python script many times, and each time it is going to read in the same values into a dictionary (hash table), and it takes a long time to create the dictionary (eg. because it's reading in a lot of data from files), then you can use a Python Pickle!

Here's a quick example:

if os.path.exists("myid.p"):
        myid = pickle.load(open("myid.p", "rb"))
    else:
        myid = read_id_files(id_dir)
        pickle.dump(myid, open("myid.p", "wb"))

No comments: