Using the data processing JSON format Python

Using the data processing JSON format Python

 

If you do not want to start from scratch to create a data format to store data, JSON is a good choice. If you know of Python, the more we redoubled. Here's an overview of how to use Python JSON data processing.

- Seth Kenlon (Author)

JSON stands for JavaScript Object Notation (JavaScript Object Notation). This is a format of data stored in key-value pairs, and it is easy to parse, format data and thus become a widely used. In addition, not because of the name and the words too literally JSON, JSON does not just use JavaScript, it can also be used in other languages. Below I will explain how it is used in Python.

First, we give a JSON example:

{
"name":"tux",
"health":"23",
"level":"4"
}

The above is a native JSON data and programming language independent. Familiar with the Python people will see this JSON data with Python in the dictionary (dictionary) looked like. This is indeed very similar between the two, if you have some understanding of Python lists and dictionary data structure, it is not difficult to understand JSON.

Use a dictionary to store data

If your application needs to store some complex data structure, consider using JSON format. Contrast text configuration file that you might have used a custom format, JSON provides a more structured storage format can be recursive. Meanwhile, Python json module has built-in data may be provided JSON import / export all required libraries analytic application. So, you do not need to write their own code for parsing JSON, and other developers do not need to parse the new data format for data when interacting with your application. For this reason, JSON when data exchange is widely adopted.

The following is the code section using nested dictionaries in Python:

#!/usr/bin/env python3
import json
# instantiate an empty dict
team = {}
# add a team member
team['tux'] = {'health': 23, 'level': 4}
team['beastie'] = {'health': 13, 'level': 6}
team['konqi'] = {'health': 18, 'level': 7}

This code declares a dictionary named team and initialized to an empty dictionary.

If you give the dictionary to add content, you first need to create a key, for example, in the above example tux, beastie, konqi, and then provide the corresponding values ​​for these key 8111. Dictionary value in the example above contains information by gamers act as one.

Dictionary is a variable variable. Data dictionary can always add, delete, or update. Such characteristics become an excellent choice that dictionary data stored in the application.

JSON format to store data

If the data stored in the dictionary needs to be persistent, these data need to write to file them. This time you need to use Python in the json module:

with open('mydata.json', 'w') as f:
json.dump(team, f)

The above code first creates a file called mydata.json, and then open the file in write mode, the file to be opened to the variable f represents (of course you can also use any name you like, such as file, output, etc.) . Json module and dump () is a method for outputting a file to a dictionary.

Export data from the application is so simple, but the exported data is structured, understandable. You can now view the exported data:

$ cat mydata.json
{"tux": {"health": 23, "level": 4}, "beastie": {"health": 13, "level": 6}, "konqi": {"health": 18, "level": 7}}

Reading data from a file JSON

If you have to export the data to a file in JSON format, and there may need to read that data back to the application. This time, the module may be used in load Python json () method:

#!/usr/bin/env python3
import json
f = open('mydata.json')
team = json.load(f)
print(team['tux'])
print(team['tux']['health'])
print(team['tux']['level'])
print(team['beastie'])
print(team['beastie']['health'])
print(team['beastie']['level'])
# when finished, close the file
f.close()

This method enables to save the file and roughly the reverse operation. F using a variable to represent an open file, then json module load () method reads the data file and place the variable team.

Where print () demonstrates how to view the read data. In the overly complex iterative dictionary dictionary key call when there might be a slight turn, but turn to, but as long as familiar with the structure of the entire data, it can slowly worked out the logic.

Of course, use print () way too inflexible. You can be rewritten to use the form for loop:

for i in team.values():
print(i)

Using JSON

As mentioned above, you can easily handle JSON data in Python. So long as you comply with JSON data model, you can choose to use JSON. JSON is very flexible and easy to use, next time use Python's time may wish to try.

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11246850.html