Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between load and loads in Python json

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what is the difference between load and loads in Python json. I hope you will get something after reading this article. Let's discuss it together.

1. Similarities

Both dump and dumps implement serialization

Both load and loads are deserialized

The process of changing a variable from memory to storable or transferable is called serialization, which is the process of transforming the state of an object into a conservable or transferable format.

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

Second, distinguish 1.load from loads (deserialization)

Load: convert characters in json format to dict and read from file (convert string to dict) for file handle

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

Loads: convert string to dict (convert string to dict) for memory objects

A = json.loads ('{'axiazo' 1111',''bounded', '2222'}') 2.dump and dumps (serialization)

Dump: convert the dict type to json string format and write to a file (easy to store)

A_dict = {'a'afia 'open' 1111 'json.dump (a_dict, open (' demo.json', 'w'))

Dumps: converting dict to string (easy to transfer)

A_dict = {'a'afiu '1111' a_str = json.dumps (a_dict)

Summary:

According to the characteristics of serialization and anti-sequence

Loads: converts string to dict

Dumps: converts dict to string

Load: converts the json format string into dict and reads the file.

Dump: converts the dict type to a json format string and saves it to a file

Third, JSON advanced 1. Serialization # uses the _ _ dict__ method class Student (object) of the class object: def _ _ init__ (self, name, age, score): self.name = name self.age = age self.score = scoreimport jsons = Student ('Bob', 20,88) print (json.dumps (s, default=lambda obj: obj.__dict__)) 2. Deserialize # Python learning communication group: 531509025def 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)) IV. Serialization and deserialization in python

Python provides two modules for serialization: cPickle and pickle. The function of the two modules is the same, the difference is that cPickle is written in C language, fast, pickle is written in pure Python, the speed is slow.

The process in which a variable changes from memory to storable or transferable is called serialization, which is called pickling in Python

Re-reading the contents of a variable from the serialized object to memory is called deserialization, or unpickling

Try: import cPickle as pickleexcept ImportError: import pickle1. Access memory objects to disk a = dict (axi1, bread2, canti3) pickle.dumps (a) # serialize objects to str and then store them in the file a = dict (axi1, bread2, cym3) pickle.dump (a, open ('a. Txtstores, 'wb')) # use dump to serialize objects directly into file-like Object, note that it is binary storage 2. Read from disk to memory object pickle.load (open ('a.txtreading,' rb')) # deserialize the object directly from file-like Object after reading this article, I believe you have some understanding of "what is the difference between load and loads in Python json". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report