and pickle deserialization json Usage

and pickle deserialization json

First, the definition

Serialization: converting the non-persistent memory and easy to transfer persistent objects and object transmission process.

Deserialize: persistent and will be transmitted to non-persistent object into the object and the transmission process.

Second, the application scenarios

Cross-platform data transmission;
changes to the object when the program remain on the run

Third, functions and modules

1. json

dumps (), dump () (serialization)

loads (), load () (deserialization)

2. pickle

dumps (), dump () (serialization)

loads (), load () (deserialization)

 
Difference dumps () and dump () is dumps () simply to give the target sequence, and the dump () will write the result to the file after the sequence of them;
Corresponding thereto, loads () and load () As the difference between loads () is a serialization of the dumps are deserialized, and the dump () reads the contents of the file from the deserialized.

Fourth, the case

序列化

info={"name":"kezi","age":22}
f=open("test.text","w")
f.write(str(info))
f.close()


反序列

info={"name":"kezi","age":22}
f=open("test.text","r")
data=eval(f.read())
f.close()
print (data['age'])


打印结果
22

 


json 简单用法

import json

info={"name":"kezi","age":22}
f=open("test.text","w")
print(json.dumps(info))
f.write(json.dumps(info))
#f.write(str(info))
f.close()

打印结果
{"age": 22, "name": "kezi"}

 

import json

f=open("test.text","r")
#data=eval(f.read())
data=json.loads(f.read())
f.close()
print (data['age'])
打印结果
22

序列化
pickle

import pickle
def si(name):
print("helle",name)
info={"name":"kezi","age":22,"func":si}
f=open("test.text","wb")
print(pickle.dumps(info))
f.write(pickle.dumps(info))
#f.write(str(info))
f.close()

打印结果
b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x04\x00\x00\x00keziq\x02X\x03\x00\x00\x00ageq\x03K\x16X\x04\x00\x00\x00funcq\x04c__main__\nsi\nq\x05u.'Si (name):
DEFthe pickle
Import

Deserialization


print("helle2",name)
f=open("test.text","rb")
#data=eval(f.read())
data=pickle.loads(f.read())
f.close()
print (data["func"]("kezi"))


打印结果
helle2 kezi
None

另一种简化写法
import pickle
def si(name):
print("helle",name)
info={"name":"kezi","age":22,"func":si}
f=open("test.text","wb")
pickle.dump(info,f) #f.write(pickle.dumps(info))
f.close()

import pickle
def si(name):
print("helle2",name)
f=open("test.text","rb")
#data=eval(f.read())
data=pickle.load(f)#data=pickle.loads(f.read())
f.close()
print (data ["func"]("kezi"))

pickle type sequence of bites result, only suitable for interaction between the Python machine.

    str json serialization result type, can be identified in multiple languages, it may be used to interact with other programming languages.
  Currently, JSON format string has become a standard format of the network transmission, it is usually used to serialize json module in the web instead of background development pickle module.
   JSON Python and the corresponding built-in data types as follows:
JSON type
Python type
{}
dict
[]
list
"string"
'Str' or u'unicode '
1234.56
int or float
true/false
True/False
null
None

 

 

Note:

 (1) serialized and deserialized in order to solve the transmission problem of persistent objects in memory;

(2) Python provides two modules pickle and json serialization and deserialization;
(3) dumps () and dump () for serialization, loads () and load () for deserialization;
(4) pickle module can serialize any object type serialization bites result, only suitable for interaction between the Python machine;
json serialization module only Python basic types, results json serialized format string, suitable for interaction between the different programming languages.
Usage and pickle json substantially the same, but be careful when json file read-write mode is "w" and "b", instead of "wb" and "rb".

 

 

Guess you like

Origin www.cnblogs.com/kezi/p/11909418.html