preface

Recently, when I was learning Python crawler, I would choose to store data in JSON format for persistent storage, which caused a question: how to use json library in Python?

Json.stringfy and json. parse used to work in JavaScript.

If you’re using the JSON library in Python, you won’t be using it!!

This problem must be solved. From this, after learning, comes the following.

content

Introduction to the

JSON is a lightweight data interchange format.

Core method

There are only four core methods of the JSON library in Python

  • json.dump
  • json.dumps
  • json.load
  • json.loads

Dump means to save, to export, to save

So it makes sense.

Dumps take apart and dump strings into character streams of data

Now, let’s look at a piece of code:

dic = {
        'name': 'tom'."age": 18."friends": (
            'mick'.'jerry'
        ),
        'text': 'hello world'
    }

    # obj --> json
    res = json.dumps(dic)
    print(res)
    # the results:
    # {"name": "tom", "age": 18, "friends": ["mick", "jerry"], "text": "hello world"}

Copy the code

Json strings use double quotes, so python’s ‘name’ becomes’ name ‘.

A tuple in Python corresponds to an arR in JSON, so the following changes occur: (‘ Mick ‘, ‘jerry’) — >[” Mick “, “jerry”]

So let’s try dump.

Dump transforms Python objects into JSON data and stores it in a file.

dic = {
        'name': 'tom'."age": 18."friends": (
            'mick'.'jerry'
        ),
        'text': 'hello world'
    }

    fp = open('./dic.json'.'w', encoding='utf-8')
    # obj --> json and write json to the file
    json.dump(dic, fp, ensure_ascii=False)
    # fp = file place specifies file location and read/write mode
Copy the code

After running this code, the dic. Json file will naturally be generated, as shown below

Loads are loads, loads are loads, loads are loads.


Dump, which converts Python objects to JSON data and saves the JSON data to a file.

Load, reads JSON data from a file and converts it to a Python object.

Dumps, Python characters are converted to json data streams. In short, Python obj — > JSON

Loads data stream into python character stream, simple, json– > python obj

Here is a set of examples:

Loads using json.loads:

    # This is a jSON-formatted string
    json_text = '{"name": "tom", "age": 18, "friends": ["mick", "jerry"], "text": "hello world"}'
    # loads = load from string
    res = json.loads(json_text)
    print(res)
    # results: {' name ':' Tom ', 'age: 18,' friends' : [' Mick 'and' jerry '], 'text' : 'hello world'}
    print(type(res))
    
      
Copy the code

Using the json.load method:

    fp = open('./dic.json'.'r', encoding='utf-8')
    Read json data from a file
    res = json.load(fp)
    print(res)
    # results: {' name ':' Tom ', 'age: 18,' friends' : [' Mick 'and' jerry '], 'text' : 'hello world'}
    print(type(res))
    
      
Copy the code

Dic. Json data is as follows:

conclusion

Loads from character stream with s added, loads from file stream without s added.

So json.load(fp), the first parameter fp specifies the location of the file, and fp is mandatory. If not, an error will be reported.

The same is true for dumps.

Special attention:

Note that Chinese is converted to Unicode by default when Chinese is saved in dump

    fp = open('./dic.json'.'w', encoding='utf-8')

    "\ cut off 20\u4e09" Chinese is converted to Unicode by default
    dic = {
        'name': 'Joe'.'age': 18
    }

    json.dump(dic, fp)
Copy the code

After running, it is obvious that Chinese has become a Unicode encoding, the solution.

Add the keyword ensure_ASCII =False so it is not automatically converted to Unicode encoding.

    fp = open('./dic.json'.'w', encoding='utf-8')

    "\ cut off 20\u4e09" Chinese is converted to Unicode by default
    dic = {
        'name': 'Joe'.'age': 18
    }
	# ensure_ASCII =False
    json.dump(dic, fp, ensure_ascii=False)
Copy the code

At this point, Chinese will display normally.

Attachment 1: Python and JSON comparison table