21 python 初学(json pickle shelve)
json:
# _author: lily # _date: 2019/1/19 import json my_dict = {'name': 'lily', 'age': 18} f = open('JSON_text', 'w') # data = json.dumps(my_dict) # f.write(data) # 上面两句等价于 json.dump(my_dict, f) f.close()
# _author: lily # _date: 2019/1/20 import json f = open('JSON_text', 'r') # data = f.read() # my_dict = json.loads(data) # 上面两句话等价于 my_dict = json.load(f) print(my_dict['name']) f.close()
pickle:
# _author: lily # _date: 2019/1/20 import pickle def foo(): print('ok') data = pickle.dumps(foo) f = open('PICKLE_text', 'wb') f.write(data) f.close()
# _author: lily # _date: 2019/1/20 import pickle def foo(): print('okok') f = open('PICKLE_text', 'rb') data = f.read() data = pickle.loads(data) data() f.close()
shelve:
# _author: lily # _date: 2019/1/20 import shelve # f = shelve.open('SHELVE_text') # f['info'] = {'name': 'lily', 'age': 18} f = shelve.open('SHELVE_text') data = f.get('info') print(data['name'])
猪猪侠要努力呀!