Python序列化模块练习

'''
#import json
序列化: 通过某种方式把数据结构或对象写入到磁盘文件中或通过网络传到其他节点的过程。
反序列化:把磁盘中对象或者把网络节点中传输的数据恢复为python的数据对象的过程。

+-------------------+---------------+
| Python | JSON |
+===================+===============+
| dict | object |
+-------------------+---------------+
| list, tuple | array |
+-------------------+---------------+
| str | string |
+-------------------+---------------+
| int, float | number |
+-------------------+---------------+
| True | true |
+-------------------+---------------+
| False | false |
+-------------------+---------------+
| None | null |
+-------------------+---------------+


#序列化
import json

a1 = {'name':'huchangxi','age':23,'love':'Python'}
b1 = json.dumps(a1) #将Python字典转换成JSON字符串
print(type(b1))
with open('json.txt','w') as f:
f.write(b1) # 等价于json.dump(dic,f)

#反序列化
with open('json.txt','r') as f:
c1 = f.read() # 等价于json.dump(dic,f)
d1 = json.loads(c1) # 反序列化成为python的字典,等价于data=json.load(f)
print(d1)
'''
posted @ 2022-04-01 22:16  呼长喜  阅读(41)  评论(0编辑  收藏  举报