On the method of serialized storage in Python

Source: Internet
Author: User
Tags serialization

This article mainly introduces the method of serialization storage in Python, the serialization storage is mainly for the memory and hard disk write operations, the need for friends can refer to the

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


1 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 occupied by the variable is recycled by the operating system. If the modified ' Bill ' is not stored on disk, the next time the program is rerun, the variable is initialized to ' Bob '.

The process of turning variables from memory into storage or transmission is called serialization, called Pickling in Python, also called serialization,marshalling,flattening in other languages, and so on.

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

Conversely, the content of the variable from the serialized object reread into memory called deserialization, that is, unpickling.

Python provides two modules to achieve serialization: Cpickle and Pickle. The function of these two modules is the same, the difference is that Cpickle is written in C language, fast, pickle is written in pure python, slow, with Cstringio and Stringio a truth. When used, first try to import cpickle, and if it fails, import pickle:


1 2 3 4 Try:import Cpickle as Pickle except importerror:import pickle

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


1 2 3 >>> d = dict (name= ' Bob ', age=20, score=88) >>> pickle.dumps (d) "dp0ns ' age ' np1ni20nss ' score ' Np2ni88nss ' name ' np3ns ' Bob ' Np4ns.

The Pickle.dumps () method serializes any object into a str and then writes the STR to the file. Alternatively, the object is serialized and written to a File-like object using another method Pickle.dump ():


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

Look at the written dump.txt file, a bunch of clutter, all of the internal information that Python keeps.

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


1 2 3 4 5 >>> f = open (' Dump.txt ', ' RB ') >>> d = pickle.load (f) >>> f.close () >>> D {' age ': 20, ' Score ': The ' name ': ' Bob '}

The contents of the variable are back again!

Of course, this variable is completely unrelated to the original variable, and they're just the same thing.

The problem with Pickle is the same as any serialization problem peculiar to all other programming languages, that is, it can only be used in Python, and maybe different versions of Python are incompatible with each other, so it's okay to save unimportant data with pickle and not successfully deserialize it.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.