Python. Module pickle. Serialization of objects. Examples of use for writing/reading information from binary files

Module pickle. Serialization of objects. Examples of use for writing/reading information from binary files


Contents


Search other websites:




1. Module pickle. Serialization/deserialization of objects

The pickle module allows you to save any Python objects in files without using unnecessary conversions. In general, the pickle module implements a binary protocol for serializing and deserializing objects. Serialization refers to the conversion of objects to a string of bytes. Deserialization involves the inverse of converting a stream of bytes into a source object.

Using the pickle module, you can:

  • implement the transformation of the hierarchy of objects into a stream of bytes (pickling). A byte stream may be, for example, a file;
  • implement back converting of the byte stream into an object hierarchy (unpickling). A stream of bytes can be obtained from a binary file or byte object.

 

2. Using pickle module. Methods dump(), load()

The pickle module is used to work with objects of type bytes and provides serialization of objects. To use pickle module methods, you must first include it

import pickle

To write an object to a file, you need to use the dump() method. The simplest method call is as follows

pickle.dump(obj, file, protocol = None, *,   fix_imports = True)

where

  • obj – saved object. This object can be a number, a string, a list, a tuple, a set, a dictionary, or another object;
  • file – binary file, in which the object can be saved.

To read an object from the file, use the load() method. In the simplest case, the call to the load() method is as follows

obj = pickle.load(file)

here obj – object obtained from file.

 

3. Read/write a list that contains real numbers. Example

Using the dump() and load() methods of the pickle module, you can save a variety of lists in files. You do not need to do additional conversions.

# Module pickle. Working with files
# Writing/reading a list containing real numbers

# 1. Source list
L = [ 2.85, 3.35, -1.77, 9.2 ]

# 2. Include module pickle
import pickle

# 3. Writing a list to a binary file
# 3.1. Open file for writing
f = open('myfile.bin', 'wb')

# 3.2. Save list L to the file f
pickle.dump(L, f)

# 3.3. Close file
f.close()

# 4. Reading list from file
# 4.1. Open file for reading
f = open('myfile.bin', 'rb')

# 4.2. Get list from file
L2 = pickle.load(f)

# 4.3. Display list L2
print('L2 = ', L2) # L2 = [2.85, 3.35, -1.77, 9.2]

# 4.4. Закрыть файл
f.close()

The result of the program

L2 = [2.85, 3.35, -1.77, 9.2]

 

4. Writing/reading a tuple containing strings. Example

Writing and reading a tuple is no different from writing and reading a list or any other object.

# Module pickle. Working with binary files
# Writing/reading a tuple

# 1. The specified tuple of strings
T = ( 'abc', 'abcd', 'cde', 'fgh hij' )

# 2. Include module pickle
import pickle

# 3. Save tuple to file
# 3.1. Open file for writing
f = open('myfile.bin', 'wb')

# 3.2. Save tuple T to file f
pickle.dump(T, f)

# 3.3. Close file
f.close()

# 4. Reading tuple from file
# 4.1. Open file for reading
f = open('myfile.bin', 'rb')

# 4.2. Read tuple from file
T2 = pickle.load(f)

# 4.3. Close the file
f.close()

# 5. Display the tuple
print('T2 = ', T2) # T2 = ('abc', 'abcd', 'cde', 'fgh hij')

The result of the program

T2 = ('abc', 'abcd', 'cde', 'fgh hij')

 

5. Writing/reading a set with different data types. Example

 

# Module pickle. Working with binary files
# Writing/reading a set

# 1. Specified set of objects of different types
M = { 2, 5.85, True, 'abcde' }

# 2. Include module pickle
import pickle

# 3. Save set to the file
# 3.1. Open file for writing
f = open('myfile.bin', 'wb')

# 3.2. Save set M to the file f
pickle.dump(M, f)

# 3.3. Close file
f.close()

# 4. Reading set from file
# 4.1. Open file for reading
f = open('myfile.bin', 'rb')

# 4.2. Read set from file
M2 = pickle.load(f)

# 4.3. Закрыть файл
f.close()

# 5. Display the set
print('M2 = ', M2) # M2 = {True, 5.85, 2, 'abcde'}

The result of the program

M2 = {True, 5.85, 2, 'abcde'}

 

6. Writing/reading objects of different types: list, tuple and dictionary. Example

This example demonstrates the capabilities of the pickle module for saving objects of different types: list, tuple and dictionary.

 

# Module pickle. Working with files
# Writing/reading objects of different types

# 1. Input data
# 1.1. Source list
L = [ True, 'abc', 2.5, 100 ]

# 1.2. Source tuple
T = ( 2.55, False, 'Hello' )

# 1.3. Dictionary with objects of different types
D = { 1:'abc', 2:2.55, 'Three':False }

# 2. Include module pickle
import pickle

# 3. Write data to binary file
# 3.1. Open file for writing
f = open('myfile.bin', 'wb')

# 3.2. Save list L to the file f
pickle.dump(L, f)

# 3.3. Save tuple to file
pickle.dump(T, f)

# 3.4. Save D to file f
pickle.dump(D, f)

# 3.5. Close file
f.close()

# 4. Reading a list from a file
# 4.1. Open file for reading
f = open('myfile.bin', 'rb')

# 4.2. Read list from file
L2 = pickle.load(f)

# 4.3. Read tuple from file
T2 = pickle.load(f)

# 4.4. Read dictionary from file
D2 = pickle.load(f)

# 4.5. Display list, tuple and dictionary
print('L2 = ', L2) # L2 = [True, 'abc', 2.5, 100]
print('T2 = ', T2) # T2 = (2.55, False, 'Hello')
print('D2 = ', D2) # D2 = {1: 'abc', 2: 2.55, 'Three': False}

# 4.6. Close the file
f.close()

 


Related topics

 


 

0
fb-share-icon20