python serialization and deserialization (json, pickle)

1. What is the sequence of & deserialization?

Serialization: a conversion dictionary, a list of instances of objects, etc., to process a content type string.

The process of converting the content into a string dictionary, a list, an object instance of the class and the like: deserialized

PS: Python common data structure may be referred to as a container. Sequence (such as lists and tuples), mapping (such as a dictionary) and a set (set) is three main container.

Scene One: We use a program to another will function in a python, how to?

Method One: function to a file, and then another python program then read out from the file.

Scene Two: Now turn read out how to convert the file into a string dictionary?

Method two: the eval () function: the string str expressions are evaluated as valid and returns the results, but there is a risk, str converted into a data structure in python recommended deserialization.

Serialization is the process of becoming dic STR (dic) and deserialization process is to become dic from str (dic).

2. Why they want to use serialization?

  The purpose of the sequence:

    1. custom object so some form of persistent storage (such as a hard disk from memory to memory)

    2. The object is passed from one place to another

    3. To make the program more maintainable

Sequence of two modules:

  json: a string (str) and python between data types (such as dictionaries, list) conversion

  pickle: python for specific types of data between the type of conversion and python

3.json

Json is a lightweight data-interchange format, based on a subset of ECMAScript. Python3 json module can be used to encode and decode data to json.

python nature: String, value in the string by double quotation marks , contains two functions:

  python Object -> json: json.dumps (python objects)

  json-> python Object: json.loads (json string)

json.dumps (): encoded data is stored in mysql string or binary data form to the hard disk.

dumps: The method of operation is output to the terminal, i.e. converted into a variable of type STR 
the dump: The method of file operations, the specific operation The json.dump (dict, Open ( ' Test ' , ' W ' ))

json.loads (): decoding the data, converts the data content of the abstract (Python object) to a string.

and is a result json.load json.loads deserialized output, dump, and dumps the output terminal of the sequence or file.

python objects (basic data types): int, float, str, list, tuple, dict

Demand: serialized, converted into a string dictionary info, test1.txt saved to a file.

ex1: with dumps () function of the sequence, writing files test1.txt f.write ().

Import JSON 
info = {
     ' name ' : ' Wendy ' ,
     ' Age ' : 22 is 
} 
F = Open ( " the test1.txt " , ' W ' )
 # json.dumps (info) converting a character string into a dictionary info from saved to hard disk memory process called serialization 
# serialization dumps function can not be serialized, can only deal with simple cross-platform data exchange 
f.write (json.dumps (info))
f.close ()

ex2: with dump () function serialization directly The json.dump () writes test1.txt file.

import json
info={
    'name':'wendy',
    'age':22
}
f=open("test1.txt",'w')
#等于f.write(json.dumps(info))
json.dump(info,f)
f.close()

4.pickle

The pickle load, loads and dump, dumps using the operation, for the first, the difference of pickle and json:

 4.1 pickle and json both serialization and de-serialization of the operation can be achieved.

 4.2 When writing to files, pickle written in an encrypted way. In opening the file, 'wb' written in binary form.

 4.3 pickle can be deserialized input to a file object class is created.

4 pickle functional modules: dump (serialization, deposit), dumps, loads (deserialization, reading), load

Import the pickle
 class the ABC: 
    A = 10
     DEF  the __init__ (Self, m, n-): 
        self.m = m 
        self.n = n- 

ABC = the ABC (1,2 ) 
RES = the pickle.dumps (ABC) # the pickle any serializable type, such as: the sequence of the class instance 
back_res = The pickle.loads (RES) Print (RES)
 Print (back_res)
 Print (back_res.a)
 # results show that 
B ' \ X80 \ x03c__main __ \ NABC \ NQ \ xOO) \ x81q \ X01 Q} \ X02 (X-\ X01 \ xOO \ xOO \ x00mq \ x03K \ x01X \ X01 \ xOO \ xOO \ x00nq \ x04K \ x02ub. ' 
< __main__.ABC object at 0x000001D7A0B31048>

 

 

 

 

Guess you like

Origin www.cnblogs.com/wendyw/p/11803688.html