What is serialization in Python? (Instance parsing)

Source: Internet
Author: User
Tags serialization
In the following article we will look at what is serialization in Python。 Find out python serializationAnd how Python serialization can play a role in Python programming.

In the process of running the program, all the variables are in memory, for example, to define a dict:

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

Variables can be modified at any time, such as changing the name to ' Bill ', but once the program is finished, the memory used by the variable is fully reclaimed by the operating system. If the modified ' Bill ' is not stored on disk, the next time you rerun the program, the variable is initialized to ' Bob '.

The process of changing a variable from memory to a storage or transfer is called serialization, and in Python it's called pickling, which is also called serialization,marshalling,flattening in other languages, and so on.

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

In turn, re-reading the variable contents from the serialized object into memory is called deserialization, i.e. unpickling.

Python provides the Pickle module for serialization.

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

>>> import pickle>>> D = dict (name= ' Bob ', age=20, score=88) >>> pickle.dumps (d) B ' \x80\x03}q\ X00 (X\X03\X00\X00\X00AGEQ\X01K\X14X\X05\X00\X00\X00SCOREQ\X02KXX\X04\X00\X00\X00NAMEQ\X03X\X03\X00\X00\X00BOBQ \x04u. '

The Pickle.dumps () method serializes any object into a bytes and then writes the bytes to the file. Or, in another way, pickle.dump () directly serializes the object and writes it to a File-like object:

>>> f = open (' Dump.txt ', ' WB ') >>> Pickle.dump (d, f) >>> F.close ()

Look at the written dump.txt file, a bunch of messy stuff, all of the information inside the object that Python holds.

When we want to read the object from disk to memory, we can read the content to a bytes, and then deserialize the object with the Pickle.loads () method, or you can directly deserialize the object from a File-like object using the Pickle.load () method. We open another Python command line to deserialize the object we just saved:

>>> f = open (' Dump.txt ', ' RB ') >>> d = pickle.load (f) >>> f.close () >>> d{' age ': +, ' SC Ore ':, ' name ': ' Bob '}

The contents of the variable are back!

Of course, this variable and the original variable are completely irrelevant objects, they are just the same content.

The problem with Pickle is the same as for all other programming language-specific serialization problems, that is, it can only be used in Python, and may be incompatible with each other in Python, so it's okay to save only those unimportant data with pickle and not successfully deserialize it.

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.