import json
json--一次性写进去,一次性读出来
dict = {1:'中国‘, 2:'b'}
f = open('f','w')
json.dump(dict, f)----显示bytes,不影响读写
json.dump(dict, f, ensure_ascii=False)----显示中文
使用json多次读入
l = [{'k': 111}, {'k2':222}, {'k3':333}] f = open('f', 'w') for dict in l: str_dict = json.dumps(dict) print(str_dict) f.write(str_dict+' ') f.close()
多次读出
import json l = [] with open('f') as f: for line in f: dict = json.loads(line.strip()) l.append(dict)
pickl---wb,rb,读入python任何数据,支持多次读入与读出
import pickle a = [1,2,3] b = [4,5,6] with open('f','rb') as f: a ,b = pickle.load(f)
import shelve f = shelve.open('shelve_f') f['key'] = {'a':1, 'b':3} f.close() import shelve f = shelve.open('shelve_f') existing = f['key'] f.close() print(existing)