In Python, the Pickle class can typically be used to serialize Python objects, while Cpickle provides a faster and simpler interface, as the python document says: "Cpickle-A faster pickle".
Cpickle can serialize any type of Python object, such as List,dict, even objects of a class. The so-called serialization, my superficial understanding is to be able to complete the preservation and can be completely reversible recovery. In Cpickle, there are four main functions that can do this work, as described in the following examples.
1, dump: Serializes the Python object to a local file.
Copy the Code code as follows:
>>> Import Cpickle
>>> data = range (1000)
>>> Cpickle.dump (Data,open ("Test\\data.pkl", "WB"))
The dump function needs to specify two parameters, the first is the Python object name that needs to be serialized, the second is a local file, and it is important to note that you need to use the Open function to start a file and specify a write operation.
2. Load: Load local file, restore Python object
Copy the Code code as follows:
>>> data = cpickle.load (open ("Test\\data.pkl", "RB"))
Like dump, you need to open a local file using the Open function and specify the "read" operation
3. Dumps: Serializes the Python object into a string variable.
Copy the Code code as follows:
>>> data_string = cpickle.dumps (data)
4. Loads: Loading a Python object from a string variable
Copy the Code code as follows:
>>> data = cpickle.loads (data_string)