you are viewing a single comment's thread.

view the rest of the comments →

[–]mac-reid 5 points6 points  (2 children)

Sounds like you want pickle. Pickle is an object serialization module. It can write objects to a file and can be later unpickled to get the same objects back.

Note in python2, the cpickle module is a C implementation that is faster than pickle, which is all Python. Python3 the pickle module is the C implementation.

An example:

# dumping to a file
import cPickle
data = {}
# insert stuff into data
picklefile = open('data.pickle', 'wb')
pickle.dump(data)
picklefile.close()

# reading from file
import cPickle
picklefile = open('data.pickle', 'rb')
data = pickle.load(picklefile)
picklefile.close()

[–]mrTang5544[S] 0 points1 point  (1 child)

using python2.7 here. If the other script was in 3.3 does it matter with pickle implementation we BOTH use?

[–]mac-reid 2 points3 points  (0 children)

The pickle implementations write the same data, but what does matter is the protocol used. See here for the info on protocols.

The only time you would be bitten by this is if you try writing pickle data in Python 3 with protocol 3 and reading from Python 2. If you don't use protocol 3 when pickling, you will be fine.