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