Python Module-Pickle module

Source: Internet
Author: User
Tags pprint

Http://blog.csdn.net/pipisorry

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

Basic interface:

Pickle.dump (obj, file, [, Protocol])
Note: Save the 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 binary protocol, and the new binary protocol introduced by version 2:2.3, which is more efficient than the previous ones. where protocols 0 and 1 are compatible with older versions of Python. The default value for protocol is 0.
File: Object to which the class files are saved. File must have a write () interface, file can be an open with a ' W ' method or an Stringio object or any other object that implements the write () interface. If protocol>=1, the file object needs to be opened in binary mode.

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

Example:
#to save a data object to a file using the Pickle moduleImportPickledata1 = {'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 ()

# To refactor a Python object from a file using the Pickle module  Import pprint, picklepkl_file = open ('data.pkl'RB ') Data1 = Pickle.load (pkl_file) pprint.pprint (data1) data2 = Pickle.load (pkl_file) pprint.pprint (data2) pkl_file.
From:http://blog.csdn.net/pipisorry

Ref:python Data Persistent Storage: Basic use of pickle modules

Python Pickle Module Learning


Python Module-Pickle module

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.