1

I have a python class that contains many objects -- some are data structures (lists, dicts etc) and some are handlers to other classes (that also contain other data structures...).

Rather than using pickle to save the entire class & its contents, I was wondering if there is a way to serialize the data of several desired objects and save them (ONLY them) in a binary file?

For example, suppose that I have the following 2 python objects:

myList = [1, 2, 3, 4, 5]
myDict = {'a' : 14, 'b' : 5, 'c' : 65}

What is the best way to save the contents of BOTH of these python objects in a binary file? ideally, I'd like to save both objects in the same binary file, and be able to load the data later on.

user3262424
  • 7,223
  • 16
  • 54
  • 84
  • You can try to build something using the [struct](http://docs.python.org/library/struct.html) module but it doesn't support saving lists or dicts as far as I know. – Julian Sep 04 '11 at 20:55
  • If they're really as simple as lists or dicts, consider json, as layed out in [How I can I read multiple JSON objects from a file/stream in Python?](http://stackoverflow.com/questions/6886283/how-i-can-i-read-multiple-json-objects-from-a-file-stream-in-python). – agf Sep 04 '11 at 21:27

2 Answers2

6

Then create a container class for them (or named tuple, or just a tuple, whatever) and pickle it (or modify the original class so that only those parts that you want are pickled). pickle is serialisation, and it is the standard Python mechanism to do it. So unless you need e.g. more interoperability between different languages, stick to it.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • Thank you. does `pickle` save data in an economical way? (I expect to deal with big objecs) – user3262424 Sep 04 '11 at 21:21
  • @user3262424: Use protocol 2, it's currently the most compact/efficient format. – Cat Plus Plus Sep 04 '11 at 21:26
  • In Python 3, there's protocol 3 which is still more efficient. In general, passing `-1` as the protocol argument will always use the most recent protocol (but may not be backwards compatible). – Thomas K Sep 04 '11 at 23:36
  • @ThomasK: Ah, yes. I still mentally default to Python 2. If the data is persisted for long periods of time, it may be best to specify the protocol explicitly, though. – Cat Plus Plus Sep 04 '11 at 23:53
  • I'm starting to use Python 3 more, although my fingers still tend to go `print x`. – Thomas K Sep 05 '11 at 11:50
1
import pickle

imelda = ('More Mayhem', 
        'Imelda May', 
        '2011',
         ((1, 'Pulling the Rug')
         (2, 'Psycho'),
         (3, 'Mayhem'),
         (4, 'Kentish Town Waltz')))

with open("imelda.pickle", "wb") as pickle_file:
    pickle.dumps(imelda, pickle_file)
dennis_ler
  • 659
  • 1
  • 9
  • 36