python serialization and deserialization (pickle, json)

python serialization and deserialization (pickle, json)

1. The so-called serialization and deserialization:

(1) the sequence of: the data is read out of memory, or may be stored into the transmission process is called serialization
(2) deserializing: the good data is stored, is converted into the original data type, called deserialization

Note : the program is run, the data are stored in memory, rather than stored (stored in the hard disk is called a file), when the program re-run, the data on the hard disk will be initialized.

2. pickle

pickle can only be used in python, and different versions of python pickle may not be compatible with each other, therefore, the general use of pickle preserve some important data can not be successfully deserialized it does not matter.

(1) dump, load

dump(obj, file, protocol=None, *, fix_imports=True)

load(file*fix_imports=Trueencoding="ASCII"errors="strict")

import pickle
it = iter(range(10))
with open("test.pk", mode="wb") as fp:
    pickle.dump(it, fp)
with open("test.pk", mode="rb") as fp:
    res = pickle.load(fp) 

  

(2) dumps, loads

dumps(obj, protocol=None, *, fix_imports=True)

loads(bytes_object*fix_imports=Trueencoding="ASCII"errors="strict")

Import the pickle 
LS = [. 1, 23 is, 65, 23 is,. 8, 12 is , 0]
 # dumps sequence of: the sequence of objects into one arbitrary bytes 
RES = the pickle.dumps (LS)
 Print (RES, type (RES)) 
RES = pickle.loads (RES)
 Print (RES)

  

3. json (dumps, loads, dump, load)

 The object is represented by a standard json javascript language, serialized data is a string, it is widely applied to other languages.

Correspondence between the data type python json data types are as follows:

json  python
object   {} dict
array   [] list
string str
int/real int/float/Enums
true/false True/False
null None

 

(1) dump, load

dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

 

dic = {'name': "黄某某", "age": 8, "sex": "男性", "family": ['爸爸', "妈妈"]}
with open("test.json", mode="w", encoding="utf-8") as fp:
    json.dump(dic, fp, ensure_ascii=False)
with open("test.json") as fp:
    res = json.load(fp)

  

(2) dumps, loads

dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Import JSON 
DIC = { ' name ' : " yellow XX " , " Age " :. 8, " Sex " : " Male " , " Family " : [ " Dad " , " Mom " ]}
 # serialization 
'' ' 
ensure_ascii If you want to display Chinese = True = False ensure_ascii 
sort_keys = True sort of dictionary key (default according to ascii from small to large) 
'' ' 
RES = json.dumps (dic, ensure_ascii = False,sort_keys=True)
print(res, type(res))
# 反序列化
res = json.loads(res)
print(res, type(res))

  

4. pickle and the difference json

'' ' 
Differences and pickle # json two modules: 
(1) type data is serialized after json str, 
    any programming language can be identified, but the data types supported by the limited 
    json continuously Load dump is not continuous, only one out of all of the data 
(2) after the data type is bytes pickle serialization, 
    all types of data can be transformed, but only for the transmission between the storage Python. 
    pickle Load continuous, multiple sets of data can be placed in the same file 
'' ' 
# successive examples dump and load 
' '' 
json: continuously dump, but only once to load all the data out deserialize, resulting in data errors 
specific to this malpractice, loads can be used to solve 
'' ' 
DIC = { ' A ' :. 1, " B " : 2 } 
with Open ( " test.json " , MODE = " W ", encoding=". 8-UTF " ) AS FP: 
    The json.dump (DIC, FP) 
    fp.write ( ' \ n- ' ) 
    The json.dump (DIC, FP) 
    fp.write ( ' \ n- ' ) 
with Open ( " test.json " ) AS FP:
     # RES = the json.load (FP) error 
    for I in FP:
         # . read line, deserialized into a dictionary, followed by cycle 
        RES = json.loads (I)
         Print (RES, type (RES))

 

Guess you like

Origin www.cnblogs.com/trent-fzq/p/10954420.html