Analysis of pickle Module in python

  • 2021-09-05 00:22:04
  • OfStack

The module introduced in this chapter is not favored in python2. The main problem is that there are security loopholes, and the problems should be solved in time. Therefore, in the current version 3, it has been properly repaired. pickle module is a serialization module, which is mainly used in tuples and lists. When tuples and lists are very close, tuples are equivalent to a list defined by const, but there is no const keyword in Python. Take these viewpoints to understand the following contents.

Module installation:

There is no need to install manually, because it is a standard module in python.

Module functions:


pickle.load()

Parameter: File name

Function: Deserializes the file contents for output.

Module uses:


import pickle
data = ['aa', 'bb', 'cc'] 
p_str = pickle.dumps(data)
print(p_str)  

Python3 pickle Module Usage

pickle (python3.x) and cPickle (modules of python2.x) correspond to the serialization and deserialization operations of java.

It is often used in the following ways:


import pickle
pickle.dump(obj,f)
pickle.dumps(obj,f)
pickle.load(f)
pickle.loads(f)

Using the pickle module, you can save Python objects directly to a file without converting them to strings or writing them to a binary file without the underlying file access operation. pickle module will create an python language-specific binary format, you basically do not need to consider any file details, it will help you clean read and write exclusive operations, only 1 need only a legal file handle.


Related articles: