Python module, python

Source: Internet
Author: User
Tags pprint

Python module, python

Http://blog.csdn.net/pipisorry

The pickle module of python implements basic data sequences and deserialization. Through the serialization operation of the pickle module, we can save the information of objects running in the program to the file for permanent storage. Through the deserialization operation of the pickle module, we can create the object stored in the last program from the file.

Basic interface:

Pickle. dump (obj, file, [, protocol])
Note: Save the object obj to the file.
Protocol is the protocol version used for serialization. The 0: ASCII protocol represents the serialized object using printable ASCII code. 1: The old binary protocol; 2: 2. the new binary protocol introduced by version 3 is more efficient than the previous one. The protocols 0 and 1 are compatible with earlier versions of python. The default value of protocol is 0.
File: the class object to which the object is saved. File must have the write () interface. file can be a file opened in 'W' mode, a StringIO object, or any other object implementing the write () interface. If protocol> = 1, the file object must be opened in binary mode.

Pickle. load (file)
Note: Read a string from the file and refactor it into the original python object.
File: class object, which has the read () and readline () interfaces.

Example:
# Use the pickle module to save the data object to the File import pickledata1 = {'A': [1, 2.0, 3, 4 +], '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 ()

# Use the pickle module to refactor the python object import pprint from the file, and 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 persistent data storage: basic use of the pickle Module

Python pickle module Learning


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.