Storing data
The program stores user-supplied information in data structures such as lists and dictionaries. When the user closes the program, you almost always want to save their information. An easy way to do this is to use the module JSON to store the data
The module JSON can dump a simple Python data structure into a file and load the data in that file when the program runs again. You can also use JSON to share data between Python programs, and more importantly, the JSON data format is not Python-specific, allowing you to share data stored in JSON format with other programming languages.
The Json format was originally developed by JavaScript, but then became a common format
Using Json.dump () and Json.load ()
Let's write a short program that stores a set of numbers, write a program that reads the numbers into memory, the first program uses Json.dump () to store this set of numbers, and the second program uses Json.load ()
The function Json.dump () accepts two arguments: the data to be stored and the file object that can be used to store the data. Here is the demo
Import= [1,2,3,5'number.json'# The data stored by the extension fingerprint file is in JSON format with open (file_name,'w') as File_object: Json.dump (Number,file_object)
We first import the JSON module, and then we create a list of numbers, we specify to store in Number.json, the file suffix is. JSON to indicate that the data stored in the file is in JSON format, and we then open the file in write mode so that JSON can see the data written to it using Json.dup () Writes the data, we do not write the output statement, open this file view, the data storage format is the same as Python
Note the Json.dump () method, passing two parameters the first one to write, the second to store the location
Write a program that reads into memory using Json.load ()
with open (filename,'r') as File_object: = json.load (file_object) Print (contents)
This is a simple way to share data between programs
Saving and reading user-generated data
For data entered by the user, it is useful to save with JSON, because if you do not store it in some way, the user's information will be lost when the program stops running. See an example
Users are prompted to enter their name when they first run the program, and remember him when they run the program again.
We'll store the names first.
IPT = input ('Enter your name''name.json'with Open (filename1,'w') as File_object: json.dump (IPT, File_object)
Then read the name stored before
With open (filename1,'r') as File_object: = json.load (file_object) Print ('wleccome%s'%name_ipt)
We merge the two programs into one, in the execution of the first to go to Name.json to try to obtain the user name, if there is no such file, with try-except processing this error, merged into the user input name and saved to Name.json
filename1 ='Name.json'Try: With open (filename1) as File_object:username=json.load (file_object)exceptFilenotfounderror:with Open (filename1,'W') as File_object2:user_ipt= Input ('Enter your name I'll rember you') Json.dump (USER_IPT,FILE_OBJECT2)Else: Print(username)
Refactoring
We often encounter code that can run, but it can be further improved by dividing the code into some kind of function that accomplishes the task, a process called refactoring. Refactoring makes the code clearer and easier to understand and easier to extend.
The main function of the above code is to greet the user, we put all the code in the name of the Greet_user () function
defGreet_user ():" "greet the user and indicate its name" "filename1='Name.json' Try: With open (filename1) as File_object:username=json.load (file_object)exceptFilenotfounderror:with Open (filename1,'W') as File_object2:user_ipt= Input ('Enter your name I'll rember you') Json.dump (USER_IPT,FILE_OBJECT2)Else: Print(username)
But Greet_user () not only greets the user, but also gets him when the user's name is stored, does not prompt the user to enter one, we will reconstruct the greet_user () function, and let the code that stores the user name be transferred to another function
defget_savename (filename):" "will get user name does not return none" " Try: with open (filename) as File_object:sname=json.load (file_object)returnsnameexceptFilenotfounderror:returnNonedefget_newname (filename):" "when not, prompt the player to enter and store" "with open (filename,'W') as File_object:newname= Input ('Enter you name I'll rember you') Json.dump (newname,file_object)returnnewnamedefgeet_username (filename):" "main function If name has read, if no input" "user_name=get_savename (filename)ifuser_name:Print(USER_NAME)Else: NewName=get_newname (filename)Print(newname) geet_username ('Name.json')
Mypython--> Advanced chapter---Storing data JSON