Detailed explanation of python reading and writing json file method

 

When we use Python daily, we often use json files. So how to use json files when writing some small programs? Today I will introduce how to read and write Json files. JSON is a data structure, which is a technique for converting strings into data. Using json can be very convenient to convert one set of data into another set of data. In Python, json files can be read using the pandas library.

  • 1. Use Python to read json files

    Using pandas to read json files, first we need to understand the structure of json files. A json file is a string, which consists of a set of data, each set of data is concatenated by strings, each character in the string is a variable, and its value depends on the last character. When reading the json file, we can convert it into a list form by passing parameters. When we write the json file, we should pay attention to the following points: (1) We need to store the json file in a variable, not a list. Each value in a variable is used to store the value of this variable, such as the variable "sort". Therefore, we're going to use slices. (2) Strings in json files usually have two types: integer and floating point. Because the format of the json file is concatenated with a series of strings, we can convert them into integer types. Doing so allows for better handling of the format of the data. Because slicing breaks up strings into smaller chunks, it's easier to work with data formats. When we read the json file, we can use slices to divide the data blocks in the json file into data blocks, and then process each block. The sort () function can also be used to read json files in Python. Here I introduce a simple sample program: (4) If we want to convert the string into a list form, we only need to add a variable to store the characters that need to be converted Just string. The following is a sample program that uses the sort () function to convert a string to a list: (5) When using the sort () function to convert a string to a list, we need to pay attention to one thing, that is, to check whether each character is contained in on the correct position. If not, we need to process them. In the above program, we can add all the characters to the error list and then merge them into one list. If it is not correct, we can also correct this error by modifying the string. (6) The last point is that you must pay attention to the integrity of the file when reading the json file. If a file is corrupted, the read job will fail. Errors in the above program are caused by corrupted files.

  • 2. Use the myspring framework to write json files

    First of all, we need to have a myspring framework, which can be used to write web applications. Here, we will use the myspring framework to write the json file. The following is a json file written with the myspring framework. We can see that there is a string in it, indicating that the string needs to be converted to JSON format. We then use this string to convert it into a list. As can be seen from the above code, it is a very simple way to convert a string into a list. Through the above code, we can see that it is very simple to write json files with the myspring framework, just enter json on the command line. If you want to learn more about the myspring framework, you can follow the public account "Python programming from entry to mastery".

    • 1. Use the read method in the myspring framework

      The read method in the myspring framework is used to read the data in the database, or read the json file when Java code is needed. First, we need to create a database object, and then use the read method to read the data in this object. In the above code, we created an object named `read`, then used the `./` keyword at the beginning of the file to name the object, and defined a variable named `json` in the file, and defined a field named `json` in it. Using the database object created by the above code, we can use it to read the Json file. In the above code, we have used the read method to read the data. It should be noted here that there are two read methods in the myspring framework: one is to read people, and the other is to write people. When we want to write people, we need to use the read method. The two methods of reading people and writing people both need to read data, and both of these methods read data from the database. As can be seen from the above code, it is simpler to use the reading method.

    • 2. Use python's built-in functions

      Here, we need to use python's built-in functions to get the content of the JSON file. Through the following code, we can see that the built-in function set() is used to get the content of the JSON file. The specific code is as follows: Here, we need to pay attention to two points. The first point is that only the content of the JSON file is obtained here, and the JAR file is not obtained. Because the JAR file contains information such as strings, lists, dictionaries, etc., which are used to store data. The second point is that we can pass the obtained string and list as parameters to the set() function. Here the parameter set() is a built-in function which accepts two parameters: a string and a list. In this example, we pass a string as an argument to the set() function and then use it to get the content of the JSON file. Two parameters are used in the code above: a string and a list. These two parameters represent a list and a string respectively.

  • 3. Use the quickset method to print the json file

    Above we have used the quickset method to read the json file and print it out, here we use the quickset method to print the json file. The print results are as follows: If you need to print multiple results, you can process them as a list, as shown below: Of course, in addition to the above three methods, there are other methods to read and write json files. For example, we can use the read method to read the content in the json file, and then use the send method to send it to the json file. Alternatively, we can split the json file into parts using the split method and then process them using the sorted method.

Common python read and write json file code:

Read the JSON file:
1. Use the load function of the json module to read the json file
```python
import json
with open('data.json', 'r') as f:
data = json.load(f)
```
2. Use the loads function of the json module to read the json string```python
import
json
with open('data.json', 'r') as f:
data_str = f.read()
data = json.loads(data_str)
```
Write to a JSON file:
1. Use the dump function of the json module to write to a json file```python import json data = {'name': 'Tom', 'age': 18}
with open('data.json ', 'w') as f: json.dump(data, f) ``` 2. Use the dumps function of the json module to convert the dictionary into a json string and write it to a file ```python import json data = {'name ': 'Tom', 'age': 18}









with open('data.json', 'w') as f:
data_str = json.dumps(data)
f.write(data_str)
```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130668983