python json和pickle讲解

1.区别

1.json是可以在不同语言之间交换数据的,而pickle只在python之间使用。

2.json只能序列化最基本的数据类型,而pickle可以序列化所有的数据类型,包括类,函数都可以序列化。

2.代码——序列化

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)

反序列

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

猜你喜欢

转载自blog.csdn.net/qq_40837794/article/details/84948330