Saving and loading objects and using brine

I am trying to save and load objects using pickle module.
First I declare my objects:

 >>> class Fruits:pass ... >>> banana = Fruits() >>> banana.color = 'yellow' >>> banana.value = 30 

After that, I open a file called "Fruits.obj" (I previously created a new .txt file and renamed "Fruits.obj"):

 >>> import pickle >>> filehandler = open(b"Fruits.obj","wb") >>> pickle.dump(banana,filehandler) 

After that, I will close the session, and I started a new one, and I set the following (attempt to access the object that he had to save):

 file = open("Fruits.obj",'r') object_file = pickle.load(file) 

But I have this message:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python31\lib\pickle.py", line 1365, in load encoding=encoding, errors=errors).load() ValueError: read() from the underlying stream did notreturn bytes 

I do not know what to do, because I do not understand this message. Does anyone know how I can upload my banana object? Thank!

EDIT: As I said, I said:

 >>> import pickle >>> file = open("Fruits.obj",'rb') 

There were no problems, but the following question:

 >>> object_file = pickle.load(file) 

And I have an error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python31\lib\pickle.py", line 1365, in load encoding=encoding, errors=errors).load() EOFError 
+55
python object pickle
Dec 25 '10 at 15:17
source share
7 answers

Regarding the second problem:

  Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python31\lib\pickle.py", line 1365, in load encoding=encoding, errors=errors).load() EOFError 

After you read the contents of the file, the file pointer will be at the end of the file - there will be no further data to read. You must rewind the file so that it is read again:

 file.seek(0) 

What you usually want to do is use the context manager to open the file and read data from it. Thus, the file will be automatically closed after completion of the block, which will also help organize file operations in significant fragments.

Finally, cPickle is a faster implementation of the brine module in C. So:

 In [1]: import cPickle In [2]: d = {"a": 1, "b": 2} In [4]: with open(r"someobject.pickle", "wb") as output_file: ...: cPickle.dump(d, output_file) ...: # pickle_file will be closed at this point, preventing your from accessing it any further In [5]: with open(r"someobject.pickle", "rb") as input_file: ...: e = cPickle.load(input_file) ...: In [7]: print e ------> print(e) {'a': 1, 'b': 2} 
+45
Dec 25 '10 at 15:50
source share

The following works for me:

 class Fruits: pass banana = Fruits() banana.color = 'yellow' banana.value = 30 import pickle filehandler = open("Fruits.obj","wb") pickle.dump(banana,filehandler) filehandler.close() file = open("Fruits.obj",'rb') object_file = pickle.load(file) file.close() print(object_file.color, object_file.value, sep=', ') # yellow, 30 
+20
Dec 25 '10 at 22:05
source share

Always open in binary mode, in this case

 file = open("Fruits.obj",'rb') 
+11
Dec 25 '10 at 15:20
source share

You also forgot to read it as a binary file.

In your part of the entry, you have:

 open(b"Fruits.obj","wb") # Note the wb part (Write Binary) 

In the read part you have:

 file = open("Fruits.obj",'r') # Note the r part, there should be ab too 

Therefore replace it with:

 file = open("Fruits.obj",'rb') 

And it will work :)




As for your second error, this is most likely the reason for not closing / syncing the file properly.

Try this bit of code to write:

 >>> import pickle >>> filehandler = open(b"Fruits.obj","wb") >>> pickle.dump(banana,filehandler) >>> filehandler.close() 

And this (unchanged) to read:

 >>> import pickle >>> file = open("Fruits.obj",'rb') >>> object_file = pickle.load(file) 

A simpler version will use the with statement.

For the record:

 >>> import pickle >>> with open('Fruits.obj', 'wb') as fp: >>> pickle.dump(banana, fp) 

For reading:

 >>> import pickle >>> with open('Fruits.obj', 'rb') as fp: >>> banana = pickle.load(fp) 
+9
Dec 25 '10 at 15:21
source share

You did not open the file in binary mode.

 open("Fruits.obj",'rb') 

Must work.

For your second error, the file is most likely empty, which means that you accidentally emptied it or used the wrong file name or something like that.

(It is assumed that you really closed the session. If not, it is because you did not close the file between writing and reading).

I checked your code and it works.

+4
Dec 25 '10 at 15:21
source share

It seems you want to keep class instances in sessions, and using pickle is a decent way to do this. However, there is a package called klepto that abstracts the saving of objects to the dictionary interface, so you can choose to sort the objects and save them in a file (as shown below) or sort the objects and save them in the database, or use json or many others instead of using pickle options. The best part about klepto is that, abstracting from the common interface, it simplifies the work, so you don’t need to memorize low-level details like saving by etching to a file or otherwise.

Note that it works for dynamically added class attributes that cannot sort ...

 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('fruits.txt') >>> class Fruits: pass ... >>> banana = Fruits() >>> banana.color = 'yellow' >>> banana.value = 30 >>> >>> db['banana'] = banana >>> db.dump() >>> 

Then we restart ...

 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('fruits.txt') >>> db.load() >>> >>> db['banana'].color 'yellow' >>> 

klepto runs on python2 and python3.

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

+2
May 21 '14 at 13:55
source share

You can use anycache to do the job for you. Assuming you have a myfunc function that instantiates:

 from anycache import anycache class Fruits:pass @anycache(cachedir='/path/to/your/cache') def myfunc() banana = Fruits() banana.color = 'yellow' banana.value = 30 return banana 

Anycache calls myfunc for the first time and parses the result to a file in cachedir using a unique identifier (depending on the function name and arguments) as the file name. For any sequential run, the pickled object is loaded.

If cachedir persists between python runs, the pickled object is taken from a previous python run.

Function arguments are also taken into account. The implemented implementation also works:

 from anycache import anycache class Fruits:pass @anycache(cachedir='/path/to/your/cache') def myfunc(color, value) fruit = Fruits() fruit.color = color fruit.value = value return fruit 
0
Nov 19 '17 at 19:50
source share



All Articles