How to save and restore multiple variables in python?

I need to save about a dozen objects to a file, and then restore them later. I tried using a for loop with brine and shelf, but that didn't work.

Edit
All the objects that I was trying to save were in the same class (I should have mentioned this before), and I did not realize that I could just save the whole class as follows:

def saveLoad(opt): global calc if opt == "save": f = file(filename, 'wb') pickle.dump(calc, f, 2) f.close print 'data saved' elif opt == "load": f = file(filename, 'rb') calc = pickle.load(f) else: print 'Invalid saveLoad option' 
+84
variables python object
Jul 04 '11 at 6:24
source share
6 answers

If you need to save multiple objects, you can simply put them in a single list or tuple, for example:

 import pickle # obj0, obj1, obj2 are created here... # Saving the objects: with open('objs.pkl', 'w') as f: # Python 3: open(..., 'wb') pickle.dump([obj0, obj1, obj2], f) # Getting back the objects: with open('objs.pkl') as f: # Python 3: open(..., 'rb') obj0, obj1, obj2 = pickle.load(f) 

If you have a lot of data, you can reduce the file size by passing protocol=-1 to dump() ; pickle will use the best available protocol instead of the standard (and more reverse) default protocol. In this case, the file should be opened in binary mode ( wb and rb , respectively).

Binary mode should also be used with Python 3 because its default protocol creates binary (i.e. non-text) data (write mode 'wb' and read mode 'rb' ).

+138
Jul 04 2018-11-11T00:
source share

There is a built-in library called pickle . Using pickle , you can dump objects into a file and load them later.

 import pickle f = open('store.pckl', 'wb') pickle.dump(obj, f) f.close() f = open('store.pckl', 'rb') obj = pickle.load(f) f.close() 
+41
Jul 04 '11 at 7:26 a.m.
source share

You should look at shelve and pickle modules. If you need to store a lot of data, it might be better to use a database

+11
Jul 04 '11 at 6:28
source share

You can use klepto , which provides constant caching to memory, disk, or database.

 dude@hilbert>$ python Python 2.7.6 (default, Nov 12 2013, 13:26:39) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from klepto.archives import file_archive >>> db = file_archive('foo.txt') >>> db['1'] = 1 >>> db['max'] = max >>> squared = lambda x: x**2 >>> db['squared'] = squared >>> def add(x,y): ... return x+y ... >>> db['add'] = add >>> class Foo(object): ... y = 1 ... def bar(self, x): ... return self.y + x ... >>> db['Foo'] = Foo >>> f = Foo() >>> db['f'] = f >>> db.dump() >>> 

Then, after restarting the interpreter ...

 dude@hilbert>$ python Python 2.7.6 (default, Nov 12 2013, 13:26:39) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from klepto.archives import file_archive >>> db = file_archive('foo.txt') >>> db file_archive('foo.txt', {}, cached=True) >>> db.load() >>> db file_archive('foo.txt', {'1': 1, 'add': <function add at 0x10610a0c8>, 'f': <__main__.Foo object at 0x10510ced0>, 'max': <built-in function max>, 'Foo': <class '__main__.Foo'>, 'squared': <function <lambda> at 0x10610a1b8>}, cached=True) >>> db['add'](2,3) 5 >>> db['squared'](3) 9 >>> db['f'].bar(4) 5 >>> 

Get the code here: https://github.com/uqfoundation

+4
May 21 '14 at 13:46
source share

The following approach seems simple and can be used with variables of various sizes:

 import hickle as hkl # write variables to filename [a,b,c can be of any size] hkl.dump([a,b,c], filename) # load variables from filename a,b,c = hkl.load(filename) 
0
Sep 18 '18 at 3:26
source share

Another approach to storing multiple variables in a selection file:

 import pickle a = 3; b = [11,223,435]; pickle.dump([a,b], open("trial.p", "wb")) c,d = pickle.load(open("trial.p","rb")) print(c,d) ## To verify 
0
Jul 04 '19 at 3:32
source share



All Articles