Python Learning--data serialization

Source: Internet
Author: User
Tags object serialization serialization definition

Json/pickle Serialization of data

Serialization definition: The process of changing a variable from memory to a storage or transport is called serialization.
Deserialization: Re-reading the contents of a variable from the serialized object into memory is called the anti-sequence hu.

--pickle of the serialization module
Note: In Python3, once serialized, and then deserialized, do not repeat multiple serialization, otherwise it cannot be deserialized.
Pickle: is a python-unique serialization module that can only be used for Python variables, which translates data objects into bytes.
Function: Used to convert between Python-specific types and Python data types.
Example:
1 Import Pickle 2 d = {'name':'jeck',' age ' ':'3print(Pickle.dumps (d))
View Code
Print out: B ' \x80\x03}q\x00 (x\x04\x00\x00\x00nameq\x01x\x08 #这里b代表为bytes
The 4 functions of pickle are as follows:
Dumps, Dump: serialization function
Loads, load: deserializing function

Example of serialization 1:
The dumps sequence is converted to a variable, the passed-in variable is serialized as a bytes, and can then be written to disk.
1 ImportPickle2D = {'name':'Jeck',' Age':' A','work':'IT'}3f = open ('fname','WB')#must be read and written in binary4 F.write (Pickle.dumps (d))5F.close ()
View Code
Example of serialization 2:
Dump serializes variables directly into a file
1 ImportPickle2D = {'name':'Jeck',' Age':' A','work':'IT'}3f = open ('fname1','WB')4 pickle.dump (d,f)5F.close ()
View Code
Deserialization Example 1:
Loads: Reads an object from disk into memory, first reads the contents to a bytes, and then deserializes it with loads
1 Import Pickle 2     f = open ('fname','rb')3      Print# deserializes the bytes through loads 4 f.close ()
View Code
Deserialization Example 2:
Load: Loads the serialized file after dump directly into the deserialization.
1 Import Pickle 2     f = open ('fname1','rb')3     Print (Pickle.load (f)) 4 F.close ()
View Code
Serialization Module--JSON
JSON: is a standard format for object serialization that is used to pass objects between different programming languages. Similar to XML.
But unlike XML representations, JSON behaves as a string, and XML is not easy to read with markup annotations. JSON is also faster than XML at speed.
Note: It is generally used to serialize common lists, dictionaries, and other commonly used sequences. If you are serializing a function in Python, the class needs to be specified separately
Methods, like Pickle, are: Dumps,dump are serialized. Loads,load to Deserialize
Function: Used to convert between string and Python data types.
Example: JSON serialized format is a string
1 ImportJSON2D = {'name':'Jeck',' Age':' A','work':'IT'}3 Print(Json.dumps (d))4 Print(Type (Json.dumps (d)))
View Code
Print:
{"Name": "Jeck", "Age": "$", "\u5de5\u4f5c": "IT"}
<class ' str ' >
Example of serialization 1:
The dumps sequence is converted to a variable, serializes the passed-in variable into a string, and can then be written to disk.
1 ImportJSON2D = {'name':'Jeck',' Age':' A','work':'IT'}3f = open ('fname','W')4 F.write (Json.dumps (d))5F.close ()
View Code
Example of serialization 2:
Dump serializes variables directly into a file
ImportJsond= {'name':'Jeck',' Age':' A','work':'IT'}f= Open ('fname1','W') Json.dump (d,f) f.close ()
Deserialization Example 1:
Loads: The object is read first and then deserialized with loads
1 Import JSON 2 f = open ('fname','r')3 Print # To deserialize a string through loads 4 F.close ()
View Code
Deserialization Example 2:
Load: Loads the serialized file after dump directly into the deserialization.
1 Import JSON 2 f = open ('fname1','rb')3 Print (Json.load (f)) 4 F.close ()
View Code
Note JSON can also serialize functions and classes in Python, but the serialized data is only the result data of a function or class.
An example of serializing a class:
1 ImportJSON2 defFun (n):3     returnN4x = Fun ('Jeck')5f = open ('fname','W')6F.write (Json.dumps (X,default=fun))#The default parameter is to tell JSON how to serialize7F.close
View Code
The general general method is:
Print (Json.dumps (x, Default=lambda obj:obj.__dict__))
The __dict__ does not need to be defined in the class, because the instance of class usually has a __dict__ attribute, which is a dictionary that stores instance variables.
Of course, functions and class serialization are generally not used.

Examples of serialization and deserialization of functions by pickle in a separate example
1: Serialization of functions
1 Import Pickle 2 def Fun (n): 3     return N 4 f = open ('fname','wb') 5 F.write (Pickle.dumps (fun)) 6 f.close  # Success
View Code
 2: Deserialization 
Import pickle
F = open (' fname ', ' RB ')
Pickle.loads (F.read ())
F.close
Error: Attributeerror:can ' t get attribute ' fun ' in <module ' __main__ ' from
is because the memory of this function has been freed since the serialized memory address, so error. This function needs to be manually added to the start of deserialization as follows:
1 Import Pickle 2 def Fun (n): 3     return # the contents of this function can be modified manually.  4 f = open ('fname','rb')  5print(pickle.loads (F.read))6 F.close
View Code
In this way, the function can be deserialized, and the individual feels that it doesn't make sense. Furthermore, the contents of the function can be modified before deserialization, so that the deserialized content is the modified content.

Note: Serialization and deserialization of pickle and JSON must be used on their own and cannot be mixed with each other.

Python Learning--data serialization

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.