Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a pkl file using dill

I have a very complex dictionary and dumping, loading directly using dill works. This is in reference to this answer. But there is a slight modification. I need to save this in some file and read that file for later use.

Here's a piece of my code:

NWORDSa is the dictionary that i saved into 'abc.pkl'

pdict1 = dill.dumps(NWORDSa)
dill.dump_session('abc.pkl')

I do not know how to read it back to get the original NWORDSa. I tried:

c = dill.load_session('abc.pkl')
NWORDS_b= dill.loads(c)  

and (wanted to save it in a variable bbn)

with open('abc.pkl', 'rb') as f:
     pickle.dump(bbn, f)  

But both do not work. Is there a better method?

like image 817
Hypothetical Ninja Avatar asked Sep 07 '14 07:09

Hypothetical Ninja


1 Answers

You're dumping the session, not the dictionary itself. I don't know if saving / loading the session is even needed - that depends on your setup.

Try:

with open(outfile, 'wb') as out_strm: 
    dill.dump(datastruct, out_strm)

and:

with open(infile, 'rb') as in_strm:
    datastruct = dill.load(in_strm)

If you need to dump the session, use dill.dump_session('session.pkl') before and dill.load_session('session.pkl') after.

like image 107
Korem Avatar answered Nov 15 '22 08:11

Korem