Pickle and Cpickle of Python core modules

Source: Internet
Author: User

the data format used by the Pickle module is Python-specific and is not backwards compatible with different versions and cannot be recognized by other languages. To interact with other languages, you can use the built-in JSON package using the Pickle module you canPythonobjects are saved directly to a file without having to convert them to a string, or write them into a binary file without the underlying file access operation. The pickle module creates a Python-language-specific binary format, and you basically don't have to consider any file details, it will help you cleanly complete the read and write exclusive operation, only need a valid file handle.
the two main functions in the Pickle module are dump () and load (). The dump () function takes a file handle and a data object as a parameter, saving the data object in a specific format to a given file. When we use the load () function to remove a saved object from a file, pickle knows how to restore those objects to their original format.

the Dumps () function performs the same serialization as the dump () function. Instead of accepting the stream object and saving the serialized data to a disk file, this function simply returns the serialized data.

the loads () function performs the same deserialization as the load () function. Instead of accepting a stream object and going to the file to read the serialized data, it takes the object directly back from the Str object that contains the serialized data.

Cpickle is pickle a faster C language compiled version.


Pickle and Cpickle are equivalent to Java serialization and deserialization operations
 
#!/usr/local/env python
#-*-Coding=utf-8-*-

if __name__ = = "__main__":
Import Cpickle

#序列化到文件
obj = 123, "Abcdedf", ["AC", 123],{"key": "Value", "Key1": "Value1"}
Print obj
#输出: (123, ABCDEDF, [AC, 123], {key1:value1, key:value})
#r Read and Write permissions R b read and write 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)
#输出: (123, ABCDEDF, [AC, 123], {key1:value1, key:value})

#序列化到内存 (saved in string format), then the object can be processed in any way, such as over the network
obj1 = cpickle.dumps (obj)
print Type (obj1)
#输出: <type str>
Print Obj1
#输出: Python-specific storage format
obj2 = cpickle.loads (obj1)
print Type (OBJ2)
#输出: <type tuple>
Print Obj2
#输出: (123, ABCDEDF, [AC, 123], {key1:value1, key:value})

Pickle and Cpickle of Python core modules

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.