Python json and pickle explanation

1. Difference

1. json can exchange data between different languages, and pickle is only used between python.

2. json can only serialize the most basic data types, while pickle can serialize all data types, including classes and functions.

2. Code-serialization

import json
dic={
    
    "name":"卢","age" :20}
with open("test.json","w",encoding="utf8") as f:
    msg=json.dumps(dic,ensure_ascii=False)
    f.write(msg)

Reverse sequence

import json
with open("test.json","r",encoding="utf8") as f:
    print(json.loads(f.read()))
    f.seek(0)
    print(json.load(f))#两个相同的结果

Guess you like

Origin blog.csdn.net/qq_40837794/article/details/84948330