On the difference between load and loads in Python json

  • 2021-12-12 08:56:55
  • OfStack

Directory 1. Similarities 2. Differences 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 objects from disk

1. Similarities

dump And dumps Serialization is implemented load And loads Implement deserialization

Serialization is the process of transforming an object's state into a saverable or transportable format.

The contents of a variable are re-read from a serialized object into memory called deserialization deserialization is the conversion of a stream to an object.

2. Differences

1. load and loads (deserialization)

load: For file handles, set the json Format characters are converted to dict Read from a file that sets the string Convert to dict )


a_json = json.load(open('demo.json','r'))


loads: For memory objects, set the string Convert to dict (Convert string to dict)


a = json.loads('{'a':'1111','b':'2222'}')

2. dump and dumps (serialization)

dump: Converts an dict type to json String format, written to file (easy to store)


a_dict = {'a':'1111','b':'2222'}
json.dump(a_dict, open('demo.json', 'w')


dumps: Converts dict to string (Easy to transmit)


a_dict = {'a':'1111','b':'2222'}
a_str = json.dumps(a_dict)

Summary:

According to the characteristics of serialization and inverse sequence,

loads: Yes string Convert to dict dumps: Is the dict Convert to string load: It is the general json Format string is converted to dict Read the file dump: Is the dict Type conversion to json Format string, save to file

3. JSON Advanced

1. Serialization


#  Use class Object's __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


#Python Learning and communication 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 to implement serialization: cPickle And pickle . The functions of these two modules are similar, and the difference is that cPickle Is written in C language, which is fast. pickle Is pure Python Writing, slow speed.

The process of changing a variable from memory to storable or transferable is called serialization, which is used in Python Calling in the middle pickling Re-reading the contents of variables from serialized objects to memory is called deserialization, that is, unpickling

try:
    import cPickle as pickle
except ImportError:
    import pickle

1. Access memory objects to disk


a = dict(a=1, b=2, c=3)
pickle.dumps(a)     #  Serialize an object to str Then save it in a file 

a = dict(a=1, b=2, c=3)
pickle.dump(a, open('a.txt', 'wb')) #  Use dump Serialize objects directly into file-like Object Note that 2 Binary storage 

2. Read from disk to memory objects


pickle.load(open('a.txt', 'rb'))    # From file-like Object Direct deserialization of objects in 

Related articles: