Pkl file upload using dill

I have very complex vocabulary and dumping, loading directly using dill works. This applies 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 is a snippet of my code:

NWORDSa

is the dictionary that I saved in 'abc.pkl'

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

      

I don't know how to read it to get the original NWORDSa

. I tried:

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

      

and (wanted to store it in bbn variable)

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

      

But both don't work. Is there a better way?

+3


source to share


1 answer


You are resetting the session, not the dictionary itself. I don't know if saving / loading the session is required - it 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 reset the session use dill.dump_session('session.pkl')

before and dill.load_session('session.pkl')

after.

+3


source







All Articles