Pickle module Data Object Persistence operation, pickle module object

Source: Internet
Author: User

Pickle module Data Object Persistence operation, pickle module object

The Pickle module is used to store persistent (serialized) data.
So I will first explain what is serialization and deserialization, what is object serialization, and object deserialization.
Generally speaking, they are defined as follows:
Serialization: save an object to a file or database field;
Deserialization: Convert the file to the original object when appropriate;
Object serialization: the process of converting an object into a byte sequence;
Object deserialization: the process of restoring the byte sequence to an object;

Usage:
Serialization is mainly required for cross-platform and Object Storage, because only strings or binary formats are allowed on the network, and files must use binary stream formats.
If you want to store a memory object, you must use serialization to convert it to xml (string), json (string), binary (Stream), and so on.

Note: file. write/read operations are string objects, while pickle. dump/load operations are structured data objects (such as lists and dictionaries ).

Common interfaces:
(1) pickle. dump (obj, file, [, protocol]) # serialize the obj object to an opened file.
Obj: Structured object
File: class file object (Object Storage)
Protocol: protocol version used for serialization. The default value of protocol is 0.
0: ASCII protocol. The serialized object is represented by printable ASCII code;
1: Old binary protocol;
2: 2. The new binary protocol introduced by version 3 is more efficient than the previous binary protocol.
(2) pickle. load (file) # serialize and read objects in file.
(3) pickle. dumps (obj [, protocol]) # returns the encapsulated object in the form of a Byte object without writing it into the file.
(4) pickle. loads (file) # Read the encapsulated object from the Byte object and return it.

Dump can serialize multiple objects to the same file, and then call load () to deserialize and read these objects in the same order.

# Examples of pickle usage: version: python 3.3.41.dump and load >>> import pickle # import pickle >>> my_list = ['abc', '123', 'Chinese ', ['20140901'] # Add a test list> pickle_file = open ('My _ list. pkl ', 'wb') # The file (my_list.pkl) must be opened in binary writable mode, that is, "wb" >>> pickle. dump (my_list, pickle_file) # Call pickle. dump method: writes my_list to the pickle_file object in binary mode >>> pickle_file.close () # closes the file object >>> import OS; OS. getcwd () # introduce the OS package and view the current python directory 'd: \ Python33 '>>> OS. listdir ('d: \ Python3 3 ') # view the files in the specified directory. You can see that a binary file named my_list.pkl has been generated [... 'My _ list. pkl ',...] >>> pickle_file2 = open ('My _ list. pkl ', 'rb') # The file (my_list.pkl) must be opened in binary readable mode, that is, "rb" >>> my_list2 = pickle. load (pickle_file2) # Call ickle. load Method to restore objects stored in binary format> print (my_list2) ['abc', '123', 'Chinese ', ['20140901'] # It can be seen that the object has been restored # improved Syntax: >>> import pickle >>> my_list = ['abc', '20160901', 'Chinese ', ['20140901'] >>> with open ('My _ list. pkl ', 'wb') as file1: # Use with o Pen as method, no need to call close () pickle. dump (my_list, file1) >>> import OS >>> OS. listdir (OS. getcwd ())[... 'My _ list. pkl ',...] >>> with open ('My _ list. pkl ', 'rb') as file2: new_list = pickle. load (file2) >>> print (new_list) ['abc', '123', 'Chinese', ['123'] 2. dumps and loads # test tuples >>> import pickle # import pickle >>> tuple1 = ('A', 1, 'qavao ') # Add A ancestor >>> tuple1 ('A', 1, 'qicao') >>> str1 = pickle. dumps (tuple1) # returns the encapsulated object in the form of a Byte object, and does not need to be written into the File> s Tr2 = pickle. loads (str1) # Read the encapsulated object from the byte Object> print (str2) ('A', 1, 'koizumi ')> type (str2) <class 'tuple'> # test string >>> str = 'Hello World! '>>> Import pickle >>> str1 = pickle. dumps (str) >>> str2 = pickle. loads (str1) >>> print (str2) Hello World! >>> Type (str) <class 'str' >>> type (str2) <class 'str'>

Comparison between Pickle and CPickle
The former is a module fully implemented using Python, And the CPickle is implemented using C, which is much faster than pickle.
It is generally recommended that you use CPickle on your computer.

References:
How to Use pickle module: http://blog.csdn.net/coffee_cream/article/details/51754484

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.