catalogue
- 1、 Same point
- 2、 Distinction
- 1. Load and loads (deserialization)
- 2. Dump and dumps (serialization)
- 3、 JSON advanced
- 1. Serialization
- 2. Deserialization
- 4、 Serialization and deserialization in Python
- 1. Access memory objects to disk
- 2. Read memory object from disk
1、 Same point
-
dump
anddumps
Both implement serialization -
load
andloads
Both implement deserialization
The process of changing a variable from memory to storable or transmissible is called serialization. Serialization is the process of transforming the object state into a storable or transmissible format.
The re reading of variable contents from serialized objects to memory is called deserialization. Deserialization is the conversion of streams into objects.
2、 Distinction
1. Load and loads (deserialization)
load:For file handles, thejson
Convert characters in format todict
, read from filestring
Convert todict
)
1
|
a_json = json.load( open ( 'demo.json' , 'r' )) |
loads:For memory objects, thestring
Convert todict
(convert string to dict)
1
|
a = json.loads( '{' a ':' 1111 ',' b ':' 2222 '}' ) |
2. Dump and dumps (serialization)
dump:Convert dict type tojson
String format, written to file (easy to store)
1
2
|
a_dict = { 'a' : '1111' , 'b' : '2222' } json.dump(a_dict, open ( 'demo.json' , 'w' ) |
dumps:Convert dict tostring
(easy transmission)
1
2
|
a_dict = { 'a' : '1111' , 'b' : '2222' } a_str = json.dumps(a_dict) |
Summary:
According to the characteristics of serialization and deserialization
-
loads:Will be
string
Convert todict
-
dumps:Will be
dict
Convert tostring
-
load:It’s Jiang Li
json
Format string converted todict
, read file -
dump:Will be
dict
Type conversion tojson
Format string, stored in file
3、 JSON advanced
1. Serialization
1
2
3
4
5
6
7
8
9
|
#Using class objects__ dict__ method class Student( object ): def __init__( self , name, age, score): self .name = name self .age = age self .score = score import json s = Student( 'Bob' , 20 , 88 ) print (json.dumps(s, default = lambda obj: obj.__dict__)) |
2. Deserialization
1
2
3
4
5
6
7
|
#Python learning exchange group: 531509025 def dict2student(d): return Student(d[ 'name' ], d[ 'age' ], d[ 'score' ]) json_str = '{"age": 20, "score": 88, "name": "Bob"}' print (json.loads(json_str, object_hook = dict2student)) |
4、 Serialization and deserialization in Python
Python
Two modules are provided for serialization:cPickle
andpickle
。 The functions of the two modules are the same, but the difference iscPickle
It’s written in C language. It’s fast,pickle
Is purePython
It’s written slowly.
- The process by which a variable becomes storable or transmissible from memory is called serialization
Python
Middle callpickling
- The re reading of variable contents from serialized objects to memory is called deserialization, i.e
unpickling
1
2
3
4
|
try : import cPickle as pickle except ImportError: import pickle |
1. Access memory objects to disk
1
2
3
4
5
|
a = dict (a = 1 , b = 2 , c = 3 ) pickle.dumps(a) #Serialize the object into STR and store it in a file a = dict (a = 1 , b = 2 , c = 3 ) pickle.dump(a, open ( 'a.txt' , 'wb' )) #Use dump to directly serialize the object into file like object. Note that it is binary storage |
2. Read memory object from disk
1
|
pickle.load( open ( 'a.txt' , 'rb' )) #Deserialize the object directly from file like object |
This is aboutPython json
That’s all for the article on the difference between load and loads in Python. For more information about the difference between load and loads in Python JSON, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you can support developeppaper in the future!