Pickle handling dictionary

Smiley 4321 ssmile03 at gmail.com
Wed Feb 29 08:29:53 CET 2012


I was successful in testing pickle with multiple objects both READ & WRITE.

I did WRITE as -
---
#!/usr/bin/python

import pickle

class apple_Class(object):
    def __init__(self, **kwds):
        self.__dict__.update(kwds)
myInst = apple_Class()
myInst.FirstString = "Apple"
myInst.SecondString = "Orange"

with open('/tmp/readfile.pkl', 'wb') as f:
    pickle.dump((myInst.FirstString, myInst.SecondString), f)
----

I did READ as -

---
#!/usr/bin/python

import pickle

class apple_Class(object):
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

myInst = apple_Class()
with open('/tmp/readfile.pkl', 'rb') as f:
    myInst.FirstString, myInst.SecondString = pickle.load(f)

print myInst.FirstString
print myInst.SecondString
---

Both worked successfully.

Now, I have to write a file having below three things -

- Having dictionary of many instances
- string as a Key
- Instances as Value.

Finally, I have to do the same thing as above to WRITE in separate file and
READ in separate file.

So, the issues is simply how to handle many instances in a dictionary with
Key and Value.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120229/f62c2f6d/attachment.html>


More information about the Python-list mailing list