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))

 

posted @ 2024-01-09 14:29  坚固的大兴  阅读(6)  评论(0)    收藏  举报