How does Python store data to json files

1 Foreword

Many programs require users to enter certain information, and programs generally store the information in data structures such as lists and dictionaries.

When the user closes the program, the information needs to be saved. A simple way is to use the module json to store the data.

The json module allows you to dump simple Python data structures into a file, and load the data in the file when the program runs again.

You can also use json to share data between Python programs. More importantly, data files in JSON (JavaScript Object Notation, originally developed by JavaScript) format can be compatible with many programming languages.

2 Use json.dump( )

Implementation code:

import json
numbers =[1,3,5,7,11]
filename ="numbers.json"withopen(filename,'w')as file_obj:
 json.dump(numbers, file_obj)

operation result:

working principle:

3 Use json.load( )

Implementation code:

import json
filename ="numbers.json"withopen(filename)as file_obj:
 numbers = json.load(file_obj)print(numbers)

operation result:

working principle:

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

How does Python store data to json files
How to delete cache files in python
How to save text files in python
How does Python handle the json module
How to delete files and directories in python
How to read and write files with Python
How does Python output integers
How does python output backslashes
How to learn python quickly
How to uninstall python plugin
3 ways to encrypt Python files
How to understand python objects
How does python update packages
How does python perform matrix operations
How to use python thread pool
How does python change the environment
How to write python configuration file
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to filter numbers in python
How does python call java classes
How to read Excel in Python
How to install Python on CentOS 8
How does python import dependency packages
How to solve python dict garbled
How to view errors in python
How does python enter interactive mode
Python reads and writes json files
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
How does python determine prime numbers
How to use SQLite in Python
How to verify successful installation of python
How to make a globe with Python
How to introduce third-party modules in Python
How does python improve the calculation speed
How to represent null values in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
R&D: How To Install Python 3 on CentOS 7
How does python call its own function
How to add custom modules in Python
How to migrate SSDB data to Tendis solution
How to process excel table with python
How to understand global variables in Python
How to view installed modules in python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to debug python program using repr
How to learn the Python time module
How does python distinguish return and yield
How to open python in different systems