Analysis on serialization storage methods in Python and python serialization

Source: Internet
Author: User

Analysis on serialization storage methods in Python and python serialization

During the running process, all variables are in the memory. For example, define a dict:

d = dict(name='Bob', age=20, score=88)

You can modify the variable at any time. For example, you can change the name to 'bill '. However, once the program ends, all the memory occupied by the variable will be recycled by the operating system. If the modified 'bill 'is not stored on the disk, the program will be re-run next time and the variable will be initialized to 'bob '.

The process of changing variables from memory to storage or transmission is called serialization. in Python, it is called pickling. In other languages, it is also called serialization, alling, flattening, and so on.

After serialization, The serialized content can be written to the disk or transmitted to another machine over the network.

In turn, it is called deserialization to re-read the variable content from the serialized object to the memory, that is, unpickling.

Python provides two modules for serialization: cPickle and pickle. The functions of these two modules are the same. The difference is that cPickle is written in C language and is fast. pickle is written in pure Python and is slow. It is similar to cStringIO and StringIO. When using the command, first try to import cPickle. If it fails, then import the pickle:

try:  import cPickle as pickleexcept ImportError:  import pickle

First, we try to serialize an object and write it into the file:

>>> d = dict(name='Bob', age=20, score=88)>>> pickle.dumps(d)"(dp0\nS'age'\np1\nI20\nsS'score'\np2\nI88\nsS'name'\np3\nS'Bob'\np4\ns."

The pickle. dumps () method serializes any object into a str, and then writes this str into the file. Alternatively, you can use pickle. dump () to serialize the Object and write it into a file-like Object:

>>> f = open('dump.txt', 'wb')>>> pickle.dump(d, f)>>> f.close()

Look at the dump.txt file, a bunch of messy content, these are the internal information of the object stored in Python.

When we want to read the object from the disk to the memory, we can first read the content into a str, and then use pickle. the loads () method deserializes an object, or you can directly use pickle. the load () method directly deserializes an Object from a file-like Object. We open another Python command line to deserialize the saved object:

>>> f = open('dump.txt', 'rb')>>> d = pickle.load(f)>>> f.close()>>> d{'age': 20, 'score': 88, 'name': 'Bob'}

The variable content is returned!

Of course, this variable is completely irrelevant to the original variable. They are only the same content.

The Pickle problem is the same as the serialization problem exclusive to all other programming languages, that is, it can only be used in Python, and different versions of Python are not compatible with each other. Therefore, only Pickle can be used to save unimportant data. It does not matter if deserialization fails.

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.