Pickle save object python

How to Use Python Pickle to Save Objects

Pickle can be used to serialize and deserialize objects. A seralized object can be saved and loaded from the disk. Pickling is a method to convert an object (list, dict, etc) to a file and vice versa.

The idea is to save one or more objects in one script and load them in another. You can also use it to save program or game states.

We will save and load using a binary file, as this saves disk space.

Serialize object

import pickle

exampleObj = {‘Python’:3,‘KDE’:5,‘Windows’:10}

fileObj = open(‘data.obj’, ‘wb’)
pickle.dump(exampleObj,fileObj)
fileObj.close()

Deserialize object

Now that the object is saved to a file, you can load it (unpickle it). In the example below we load the object from the file.

import pickle

fileObj = open(‘data.obj’, ‘rb’)
exampleObj = pickle.load(fileObj)
fileObj.close()
print(exampleObj)

This will show you the previously saved object:

python pickle

Exercise

Enumerate Explained (With Examples)

Источник

How to Use Pickle to Save Objects in Python

Two rows of pickles

Albert Lukaszewski, Ph.D., is a veteran computer programmer, software engineer, and author, who specializes in the Python language.

Pickle, which is part of the Python library by default, is an important module whenever you need persistence between user sessions. As a module, pickle provides for the saving of Python objects between processes.

Whether you are programming for a database, game, forum, or some other application that must save information between sessions, pickle is useful for saving identifiers and settings. The pickle module can store things such as data types such as booleans, strings, and byte arrays, lists, dictionaries, functions, and more.

Note: The concept of pickling is also known as serialization, marshaling, and flattening. However, the point is always the same—to save an object to a file for later retrieval. Pickling accomplishes this by writing the object as one long stream of bytes.

Pickle Example Code in Python

To write an object to a file, you use a code in the following syntax:

import pickle 
object = Object()
filehandler = open(filename, 'w')
pickle.dump(object, filehandler)

Here’s how a real-world example looks:

import pickle 
import math
object_pi = math.pi
file_pi = open('filename_pi.obj', 'w')
pickle.dump(object_pi, file_pi)

This snippet writes the contents of object_pi to the file handler file_pi, which in turn is bound to the file filename_pi.obj in the directory of execution.

To restore the value of the object to memory, load the object from the file. Assuming that pickle has not yet been imported for use, start by importing it:

import pickle 
filehandler = open(filename, 'r')
object = pickle.load(filehandler)

The following code restores the value of pi:

import pickle 
file_pi2 = open('filename_pi.obj', 'r')
object_pi2 = pickle.load(file_pi2)

The object is then ready for use once again, this time as object_pi2. You can, of course, reuse the original names, if you prefer. This example uses distinct names for clarity.

Things to Remember About Pickle

Keep these things in mind when using the pickle module:

  • The pickle protocol is specific to Python – it’s not guaranteed to be cross-language compatible. You most likely cannot transfer the information to make it useful in Perl, PHP, Java, or other languages.
  • There is also no guarantee of compatibility between different versions of Python. IThe incompatibility exists because not every Python data structure can be serialized by the module.
  • By default, the latest version of the pickle protocol is used. It remains that way unless you manually change it.

Tip: Also find out how to use shelve to save objects in Python for another method of maintaining object continuity.

Источник

Читайте также:  Create json objects in java
Оцените статью