About the difference between load and loads in Python JSON

Time:2022-5-14
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, thejsonConvert characters in format todict, read from filestringConvert todict)

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

loads:For memory objects, thestringConvert todict (convert string to dict)

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

2. Dump and dumps (serialization)

dump:Convert dict type tojsonString 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 bestringConvert todict
  • dumps:Will bedictConvert tostring
  • load:It’s Jiang LijsonFormat string converted todict, read file
  • dump:Will bedictType conversion tojsonFormat 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

PythonTwo modules are provided for serialization:cPickleandpickle。 The functions of the two modules are the same, but the difference iscPickleIt’s written in C language. It’s fast,pickleIs purePythonIt’s written slowly.

  • The process by which a variable becomes storable or transmissible from memory is called serializationPythonMiddle callpickling
  • The re reading of variable contents from serialized objects to memory is called deserialization, i.eunpickling
?
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 jsonThat’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!

Recommended Today

Openssh upgrade (from openssh_7.4p1 to openssh_8.4p1) [operation Script]

Openssh upgrade (from openssh_7.4p1 to openssh_8.4p1) ##–Status before upgrade [[email protected] ~]# ssh -V OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017 ##–Start upgrade wget -c https://ftp.openssl.org/source/openssl-1.1.1h.tar.gz wget -c https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-8.4p1.tar.gz yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel pcre-devel pam-devel yum install -y pam* zlib* mv /usr/bin/openssl /usr/bin/openssl_bak mv /usr/include/openssl /usr/include/openssl_bak tar xfz openssl-1.1.1h.tar.gz cd openssl-1.1.1h […]