Pickle and cPickle In the python core module, picklecpickle

Source: Internet
Author: User

Pickle and cPickle In the python core module, picklecpickle
The pickle module uses a data format dedicated to python. Different versions are not backward compatible and cannot be recognized by other languages. To interact with other languages, you can use the built-in json package to use the pickle module. You can directly save Python objects to files without converting them into strings, you do not need to write the underlying file access operations into a binary file. The pickle module will create a binary format dedicated to the python language. Basically, you don't need to consider any file details. It will help you perform read and write exclusive operations in a clean manner, the only requirement is a valid file handle.
The two main functions in the pickle module are dump () and load (). The dump () function accepts a file handle and a data object as parameters, and saves the data object to a specified file in a specific format. When we use the load () function to extract saved objects from a file, pickle knows how to restore these objects to their original format.

The dumps () function executes the same serialization as the dump () function. Instead of accepting stream objects and saving serialized data to disk files, this function simply returns serialized data.

The loads () function executes the same deserialization as the load () function. Instead of accepting a stream object and reading serialized data from a file, it accepts the str object that contains the serialized data and directly returns the object.

CPickle is pickle to get a faster C language version.


Pickle and cPickle are equivalent to java serialization and deserialization operations.
 
#! /Usr/local/env python
#-*-Coding = UTF-8 -*-

If _ name _ = "_ main __":
Import cPickle

# Serialize to a file
Obj = 123, "abcdedf", ["ac", 123], {"key": "value", "key1": "value1 "}
Print obj
# Output: (123, abcdedf, [ac, 123], {key1: value1, key: value })
# R read and write permissions r B reads and writes to binary files
F = open (r "d: a.txt", "r ")
CPickle. dump (obj, f)
F. close ()
F = open (r "d: a.txt ")
Print cPickle. load (f)
# Output: (123, abcdedf, [ac, 123], {key1: value1, key: value })

# Serialize to memory (saved in string format), and then the object can be processed in any way, for example, transmitted over the network
Obj1 = cPickle. dumps (obj)
Print type (obj1)
# Output: <type str>
Print obj1
# Output: python-specific storage format
Obj2 = cPickle. loads (obj1)
Print type (obj2)
# Output: <type tuple>
Print obj2
# Output: (123, abcdedf, [ac, 123], {key1: value1, key: value })
Which version of python introduces pickle and cPickle?

Pickle was initially

CPickle was originally
 
What is the usage of try‑t in python?

Here, Pickle and cPickle are module names that cannot be in lower case. cPickle and Pickle are two python modules that provide data persistence methods. cPickle is the C language implementation of Pickle.
Try:
Import cPickle as p # If you can import the cPickle module, import it and name it "p ".
Except t:
Import Pickle as p # if an error occurs when importing the cPickle module, import the Pickle and name it p.

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.