"Learning Python with You Hand in Hand" 35-Data Storage

In the previous article "Learning Python with You Hand in Hand" 34-File Writing , we learned two file writing methods, and at the same time, demonstrated the difference between different file opening modes for read and write operations.

The storage of the data that will be learned today is actually a kind of file writing, but the previous writing is mainly string, and today we will use a specific data format for file storage and reading, this format is ——JSON.

1、JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format with a concise and clear hierarchical structure, which is not only convenient for people to read and write, but also easy for machine analysis and generation, which can effectively improve network transmission efficiency. JSON was originally developed for JavaScript, but then became a common standardized data format and is widely used in many programming languages ​​including Python.

Using JSON, a standardized data format for data storage, can be easily transmitted and shared in different programming languages ​​and networks. In one of the important application branch crawlers of Python, JSON format is used to crawl and store web page information.

There is no need to know much about the specific format of JSON at present, if necessary, we will introduce it later.

2. JSON data storage

The file writing methods we learned before are write() and writelines(). The content written here is mainly in string format. If you want to write or store data in JSON format, you need to use the json.dump() function.

The json.dump() function includes two parameters, one is the content of the data to be stored, and the other is the file name (including the path) used to store the data. Just like when we write files, before storing JSON data, we also need to open the file with open() and select the appropriate open mode.

In addition, since the dump() function is not a built-in function of Python, you need to import the json module before it can be used. The previous json.dump() is actually a combination of module name and function name. For this section, please refer to the content introduced in "Learning Python with You Hand in Hand" 30-Module .

In [1]: import json
        path = 'lesson/text/blank.txt'
        data = '《手把手陪您学Python》1——为什么要学Python?'   # 写入字符串
        with open(path, encoding = 'utf-8', mode = 'w+') as file_object:
            json.dump(data, file_object)

After using json.dump() to store data, there is also no output. If you open the file, you can see that the data we just stored has been saved in the file.

3. Reading JSON data

Reading JSON requires a specific function json.load() in the json module, so that we can read the data just stored.

In [2]: import json
        path = 'lesson/text/blank.txt'
        with open(path, encoding = 'utf-8') as file_object:
            print(json.load(file_object))
Out[2]: 《手把手陪您学Python》1——为什么要学Python?

4. Writing and reading of other data types

In addition to the strings just demonstrated, JSON also supports the storage of arrays and dictionaries, and these two data types are the most important data structure components of the JSON format.

In [3]: import json
        path = 'lesson/text/blank.txt'
        data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]   # 写入数组
        with open(path, encoding = 'utf-8', mode = 'w+') as file_object:
            json.dump(data, file_object)
            file_object.seek(0)
            print(json.load(file_object))
Out[3]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
In [4]: import json
        path = 'lesson/text/blank.txt'
        data = {'1': 'a', '2': 'b', '3': 'c'}   # 写入字典
        with open(path, encoding = 'utf-8', mode = 'w+') as file_object:
            json.dump(data, file_object)
            file_object.seek(0)
            print(json.load(file_object))  
Out[4]: {'1': 'a', '2': 'b', '3': 'c'}

The above is the storage and reading process based on the JSON data format. Since we have learned how to read and write files before, it will be relatively easy to understand this part of the content.

In the next article, we will introduce error and exception handling methods, so stay tuned.

 

image

 


Thanks for reading this article! If you have any questions, please leave a message and discuss together ^_^

To read other articles in the "Learning Python with You Hand in Hand" series, please follow the official account and click on the menu selection, or click the link below to go directly.

"Learning Python with You Hand in Hand" 1-Why learn Python?

"Learning Python with you hand in hand" 2-Python installation

"Learning Python with You Hand in Hand" 3-PyCharm installation and configuration

"Learning Python with You Hand in Hand" 4-Hello World!

"Learning Python with You Hand in Hand" 5-Jupyter Notebook

"Learning Python with You Hand in Hand" 6-String Identification

"Learning Python with You Hand in Hand" 7-Index of Strings

"Learning Python with You Hand in Hand" 8-String Slicing

"Learning Python with You Hand in Hand" 9-String Operations

"Learning Python with You Hand in Hand" 10-String Functions

"Learning Python with You Hand in Hand" 11-Formatted Output of Strings

"Learning Python with You Hand in Hand" 12-Numbers

"Learning Python with You Hand in Hand" 13-Operation

"Learning Python with You Hand in Hand" 14-Interactive Input

"Learning Python with You Hand in Hand" 15-judgment statement if

"Learning Python with You Hand in Hand" 16-loop statement while

"Learning Python with You Hand in Hand" 17-the end of the loop

"Learning Python with You Hand in Hand" 18-loop statement for

"Learning Python with You Hand in Hand" 19-Summary of the first stage

"Learning Python with You Hand in Hand" 20-List

"Learning Python with You Hand in Hand" 21-Tuples

"Learning Python with You Hand in Hand" 22-Dictionary

"Learning Python with You Hand in Hand" 23-Built-in Sequence Function

"Learning Python with You Hand in Hand" 24-Collection

"Learning Python with You Hand in Hand" 25-List Comprehension

"Learning Python with You Hand in Hand" 26-Custom Functions

"Learning Python with You Hand in Hand" 27-Parameters of Custom Functions

"Learning Python with You Hand in Hand" 28-the return value of a custom function

"Learning Python with You Hand in Hand" 29-Anonymous Functions

"Learning Python with You Hand in Hand" 30-Module

"Learning Python with You Hand in Hand" 31-File Opening

"Learning Python with You Hand in Hand" 32-File Reading

"Learning Python with You Hand in Hand" 33-closing the file

"Learning Python with You Hand in Hand" 34-File Writing

For Fans: Follow the "also said Python" public account, reply "hand 35", you can download the sample sentences used in this article for free.

Also talk about Python-a learning and sharing area for Python lovers

Guess you like

Origin blog.csdn.net/mnpy2019/article/details/111658263