SoFunction
Updated on 2024-10-29

Python pickle module for object serialization

This article introduces the Python pickle module to achieve object serialization, the text of the sample code through the introduction of the very detailed, for everyone to learn or work with certain reference learning value, you can refer to the following friends

corresponds English -ity, -ism, -ization

Serialize Python objects for easy storage and transmission

Python object serialization to type bytes

(obj) Convert Python object to bytes type

(str) Reduces the converted bytes to an object.

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pickle
>>> mydict = {'id':123, 'name':'abc'}
>>> pickle_str = (mydict)
>>> pickle_str
b'\x80\x03}q\x00(X\x02\x00\x00\x00idq\x01K{X\x04\x00\x00\x00nameq\x02X\x03\x00\x00\x00abcq\x03u.'
>>> newdict = (pickle_str)
>>> newdict
{'id': 123, 'name': 'abc'}

Python Object Serialization for Writing to Files

(obj, file)

Multiple objects can be stored and read in the order in which they are stored.

>>> import pickle
>>> mydict = {'id':123, 'name':'abc'}
>>> file = open('','wb')
>>> (mydict, file)
>>> ()

Reading Python Objects Deserialized from a File

(obj, file)

>>> import pickle
>>> file = open('','rb')
>>> newdict = (file)
>>> ()
>>> newdict
{'id': 123, 'name': 'abc'}

This is the whole content of this article.