Python基础-序列化 json,pickle
json中duamps
# 特殊的参数 l1 = [1, 2, 3, {'name': 'alex'}] # dump load 只能写入文件,只能写入一个数据结构 # with open('json文件1',encoding='utf-8',mode='w') as f1: # json.dump(l1,f1) # 读取数据 # with open('json文件1',encoding='utf-8') as f2: # l1 = json.load(f2) # print(l1,type(l1)) # 一次写入文件多个数据怎么做? # 错误示例: # dic1 = {'username': 'alex'} # dic2 = {'username': '太白'} # dic3 = {'username': '大壮'} # with open('json文件1',encoding='utf-8',mode='w') as f1: # json.dump(dic1,f1) # json.dump(dic2,f1) # json.dump(dic3,f1) # 读取数据 # with open('json文件1',encoding='utf-8') as f1: # print(json.load(f1)) # print(json.load(f1)) # print(json.load(f1)) # 正确写法: dic1 = {'username': 'alex'} dic2 = {'username': '太白'} dic3 = {'username': '大壮'} # with open('json文件1',encoding='utf-8',mode='w') as f1: # f1.write(json.dumps(dic1) + '\n') # f1.write(json.dumps(dic2) + '\n') # f1.write(json.dumps(dic3) + '\n') # with open('json文件1',encoding='utf-8') as f1: # for i in f1: # print(json.loads(i))
星辰大海