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