Python Pickle Module Usage Example _python

Source: Internet
Author: User
Tags serialization pprint

Python's pickle module implements basic data sequences and deserialization. Through the serialization operation of the Pickle module, we can save the object information running in the program to the file, store it permanently, and through the deserialization operation of the Pickle module, we can create the last program saved object from the file.

Basic interface:

Copy Code code as follows:

Pickle.dump (obj, file, [, Protocol])

Note: Save object obj to file.

Protocol is the protocol version used for serialization, the 0:ASCII protocol, the serialized object is represented by printable ASCII code, 1: The old-fashioned binary protocol, and the new binary protocol introduced in version 2:2.3 is more efficient than before. where protocols 0 and 1 are compatible with the old version of Python. The default value for protocol is 0.

File: The class file object to which the object is saved. File must have a write () interface, and file can be a file opened in ' W ' mode or an Stringio object or any other object that implements the write () interface. If protocol>=1, the file object needs to be open in binary mode.

Pickle.load (file)
Note: Reads a string from file and reconstructs it as the original Python object.
File: Class file object, with read () and ReadLine () interface.

A Simple Code

Copy Code code as follows:

#使用pickle模块将数据对象保存到文件 Import Pickle

Data1 = {' A ': [1, 2.0, 3, 4+6j], ' B ': (' string ', U ' Unicode string '), ' C ': None}

Selfref_list = [1, 2, 3]
Selfref_list.append (Selfref_list)

Output = open (' Data.pkl ', ' WB ') # Pickle dictionary using protocol 0. Pickle.dump (data1, Output) # Pickle the list using the highest protocol available. Pickle.dump (selfref_list, output,-1)

Output.close ()


Copy Code code as follows:

#使用pickle模块从文件中重构python对象 Import Pprint, pickle

Pkl_file = open (' data.pkl ', ' RB ')

Data1 = Pickle.load (pkl_file)
Pprint.pprint (DATA1)

Data2 = Pickle.load (pkl_file)
Pprint.pprint (DATA2)

Pkl_file.close ()

Related Article

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.