Python full stack development-json and pickle modules (serialization of data)

Source: Internet
Author: User
Tags serialization

One What is serialization?

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

Why serialize?

  1. Persistent Save State

It is necessary to know that the execution of a software/program is dealing with a series of state changes, in which the ' state ' is stored in memory in the form of a variety of structured data types (which can also be simply understood as variables).

Memory is unable to permanently save the data, when the program has been running for some time, we power down or restart the program, in memory of the program in the previous period of time data (structure) was emptied.

Save all the data in the program's current memory (to a file) before powering down or restarting the program, so that the next time the program executes the data before it can be loaded from the file, it will continue to execute, which is serialization.

Specifically, for example, the virtual machine state hangs, and so on.

2. Cross-platform data interaction

After serialization, not only can the serialized content be written to disk, but also can be transmitted over the network to other machines, if the sending and receiving parties agreed to use a serialized format, then break the platform/language differences brought about by the limitations, to achieve cross-platform data interaction.

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

 If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.

JSON represents objects that are standard JavaScript language objects, and JSON and Python have built-in data types that correspond to the following:

Second, JSON serialization

1. Serialization of dumps and loads deserialization

Serialization of Dumps ()

ImportJson#Importing JSON modulesInfo= {     'name':"Qianduoduo",     " Age": 22,}with Open ("Test.json","W") as F:#write in normal modeF.write (Json.dumps (info))#Convert a memory object to a string#Write to File  #content in the Test.json file{"name":"Qianduoduo"," Age": 22}

Loads () deserialization

 #   import JSON module   with open ( "  test.json  , "  r  ) as F: #   read  data = Json.loads (F.read ()) in normal mode #   Deserialize  print  (Data[ age  ]) #  date.get ("Age")  #   output  22 

2. Dump serialization and load deserialization

Dump () serialization

ImportJSON Info= {    'name':"Qianduoduo",     " Age": 22}with Open ("Test.json","W") as F:#the file opens in a written wayJson.dump (INFO,F)#The 1th parameter is the memory data object, and the 2nd parameter is the file handle#content in the Test.json file{"name":"Qianduoduo"," Age": 22}

Load () deserialization

Import JSON with open ("text.txt","R") as F:   # Open the file as read data = Json.load (f)  # input File object print(Data.get ("  Age "))   # date["Age"] # output 22

3. Serialization function (not supported, direct error)

Summarize:

1. Dumps and loads are used in pairs, and dump and load are used in pairs.

2, dumps and loads because the serialization is the content, so the following to add S, but dump and load serialized content is the object, so singular.

3, JSON can only handle simple data types, such as: dictionaries, lists, strings, etc., can not handle complex data types such as functions.

Why can't you handle complicated because Python and other language-defined functions, classes are completely different, and features are different

4, JSON is common in all languages, all languages support JSON, if we need Python and other languages to interact with the data, then in JSON format.

Iii. Serialization of Pickle

1. Serialization of dumps and loads deserialization

Serialization of Dumps ()

ImportPickle Info= {    'name':"Qianduoduo",    " Age": 22,} with open ("Test.json","WB") as F:#write in binary formdata = Pickle.dumps (info)#serializing into a stringF.write (data)#writing to the Text.txt file#output to the content in the Test.json file we can't read it. Two-tier system?} Q (X nameqxqianduoduoqx ageqku.

Loads () deserialization

Import Picklewith Open ("text.txt","RB"#  Read in binary mode data = Pickle.loads (F.read ())   # Deserialize the operation print(Data.get ("  Age ")    #date[' age '  is the same # output 22

2. Dump serialization and load deserialization

Dump () serialization

ImportPickleinfo= {    'name':"Qianduoduo",    " Age": 22,}with Open ("Text.txt","WB") as F:pickle.dump (info,f)#Serialization of#Output?} Q (X nameqxqianduoduoqx ageqku.

Load () deserialization

Import Picklewith Open ("text.txt","RB"= Pickle.load (f)  # deserialized into memory object print(Data.get ("Age" )     #or   date["age"   ) # output 22

3. Serialization functions

Serialization of

ImportPickledefSayhi (name):#function    Print("Hello:", name) info= {   'name':"Duoduo",   " Age": 22,   "func": Sayhi#The value of "func" corresponds to Sayhi, which is the function name if Sayhi Plus () executes the function}with Open ("Test.json","WB") as F:data=Pickle.dumps (Info) f.write (data)#Output Test.json?} Q (X nameqxqianduoduoqx ageqkx funcqc__main__sayhi

Deserialization

ImportPickledefSayhi (name):#This function must be written in deserialization, otherwise it will be an error because the function is not loaded into memory at the time of loading    Print("Hello:", name) with open ("Test.json","RB") as F:data=pickle.loads (F.read ())Print(data[" Age"]) Data.get ("func")("Qianduoduo")#Execute function Sayhi#Output22Hello:qianduoduo#The logic in the output function body can also be changed, but the function name must be the same, and this is a place to note

Summary:

1. JSON values support simple data types, pickle supports all Python data types.

2. Pickle can only support serialization and deserialization of Python itself and cannot be used as a data interaction with other languages, and JSON can.

3, pickle serialization is the entire data object, so when the deserialization function, the logic in the function body changed, is followed by the new function body logic.

4, Pickle and JSON in 3.0 can only dump once and load once, dump in 2.7 can dump several times, load several times, anyway, and only remember, only need to dump once, load once can.

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.

Python full stack development-json and pickle modules (serialization of data)

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.