Python-Day20-json序列化
# 序列华 -- 转化一个字符串数据类型 # 序列 -- 字符串 import json # 从数据类型 --> 字符串的过程 序列化 # 从字符串 --> 数据类型的过程 反序列化 dic = {'name':'张三', 'age':'66'} print(dic,type(dic)) dic1 = json.dumps(dic,ensure_ascii=False) print(dic1,type(dic1)) dic2 = json.loads(dic1) print(dic2,type(dic2)) with open('test.txt','w',encoding='utf-8') as f: f.write(dic1) f.write('\n') f.write(dic1) f.write('\n') with open('test.txt','r',encoding='utf-8') as f: for line in f: a = json.loads(line.strip()), print('读取',a,type(a))

浙公网安备 33010602011771号