Reading and writing json files in python (for corresponding deep learning json files)

JSON (JavaScript Object Notation) is a lightweight data interchange format . <See Baidu Encyclopedia>

Nowadays, json files are generally used as text type files in work, and frequent operation of json files has become an indispensable part of daily work. Here is an introduction to python's related operations on json files.

Anyone who is familiar with python knows that json is a module in python, you only need to import it to use the relevant basic operations normally!

  • JSON has four basic operations: dumps, dump, loads, load

They appear in pairs. For example, dumps and loads are a pair, mainly for data conversion operations.
Dump and load are a pair, mainly for adding an operation on files.

  • dumps: Convert data types to string types
  • loads: convert string type to data type
  • dump: Convert the data type to a string type and store it in a file
  • load: load the file and convert the string type into a data type

However, the data type here generally refers to the dictionary type

See the relevant code operation below!

dumps

insert image description here
As shown in the picture above: import the json module, use the original dictionary type definition, and perform type conversion through dumps. You can see the comparison of the two variables in the picture, and after conversion, it becomes a string type!

loads

insert image description here
As shown in the picture above: re-transform the original character creation type into a dictionary type through loads!

dump

insert image description here
As shown in the figure above: first check whether there are files in this directory, and find that there are no files, and then convert the new_dict dictionary type into a string type through the dump operation and write it into the myjson.json file. Finally, cat checks and finds that there are files in the path and the content is also written go in!

load

insert image description here
As shown above: use load to load the file, convert the string type into a dictionary type and print it out!

Like to give a thumbs up!
Welcome to pay attention to the official account <Mu Muxi's Algorithm Notes> Come and play with me, mua~ The
official account will update interesting image algorithms from time to time! ! !

Guess you like

Origin blog.csdn.net/ganbelieve/article/details/106643115