三者都是对数据类型进行序列化,然后存入文件
pickle 只支持python,但对python里的所有数据类型都支持
shelve同上,只是存取更方便,将数据序列化以字典的形式存取进文件中,linux
json对所有语言都支持,可读性较高,但对于python来说只支持常规的数据类型(str,int,dict,set,list,tuple)
import pickle
menu_dump = pickle.dumps(menu) print(menu_dump) menu_dump_load = pickle.loads(menu_dump) print(menu_dump_load) with open('menu_dump.pkl','wb') as file_1: pickle.dump(menu,file_1) with open('menu_dump.pkl','rb') as file_2: content = pickle.load(file_2) print(content) #b'x80x04x95xx01x00x00x00x00x00x00}x94(x8cx06xe5x8cx97xe4xbaxacx94}x94(x8cx06xe6xb5xb7xe6xb7x80x94}x94(x8c xe4xbax94xe9x81x93xe5x8fxa3x94}x94(x8cx04sohox94}x94x8cx06xe7xbdx91xe6x98x93x94}x94x8cx06googlex94}x94ux8c xe4xb8xadxe5x85xb3xe6x9dx91x94}x94(x8c xe7x88xb1xe5xa5x87xe8x89xbax94}x94x8cx0cxe6xb1xbdxe8xbdxa6xe4xb9x8bxe5xaexb6x94}x94x8cx05youkux94}x94ux8cx06xe4xb8x8axe5x9cxb0x94}x94x8cx06xe7x99xbexe5xbaxa6x94}x94sux8cx06xe6x98x8cxe5xb9xb3x94}x94(x8cx06xe6xb2x99xe6xb2xb3x94}x94(x8c xe8x80x81xe7x94xb7xe5xadxa9x94}x94x8cx06xe5x8cx97xe8x88xaax94}x94ux8c xe5xa4xa9xe9x80x9axe8x8bx91x94}x94x8c xe5x9bx9exe9xbex99xe8xa7x82x94}x94ux8cx06xe6x9cx9dxe9x98xb3x94}x94x8cx06xe4xb8x9cxe5x9fx8ex94}x94ux8cx06xe4xb8x8axe6xb5xb7x94}x94(x8cx06xe9x97xb5xe8xa1x8cx94}x94x8cx0cxe4xbaxbaxe6xb0x91xe5xb9xbfxe5x9cxbax94}x94x8c xe7x82xb8xe9xb8xa1xe5xbax97x94}x94ssx8cx06xe9x97xb8xe5x8cx97x94}x94x8c xe7x81xabxe8xbdxa6xe7xabx99x94}x94x8cx06xe6x90xbaxe7xa8x8bx94}x94ssx8cx06xe6xb5xa6xe4xb8x9cx94}x94ux8cx06xe5xb1xb1xe4xb8x9cx94}x94u.' #{'北京': {'海淀': {'五道口': {'soho': {}, '网易': {}, 'google': {}}, '中关村': {'爱奇艺': {}, '汽车之家': {}, 'youku': {}}, '上地': {'百度': {}}}, '昌平': {'沙河': {'老男孩': {}, '北航': {}}, '天通苑': {}, '回龙观': {}}, '朝阳': {}, '东城': {}}, '上海': {'闵行': {'人民广场': {'炸鸡店': {}}}, '闸北': {'火车站': {'携程': {}}}, '浦东': {}}, '山东': {}} #{'北京': {'海淀': {'五道口': {'soho': {}, '网易': {}, 'google': {}}, '中关村': {'爱奇艺': {}, '汽车之家': {}, 'youku': {}}, '上地': {'百度': {}}}, '昌平': {'沙河': {'老男孩': {}, '北航': {}}, '天通苑': {}, '回龙观': {}}, '朝阳': {}, '东城': {}}, '上海': {'闵行': {'人民广场': {'炸鸡店': {}}}, '闸北': {'火车站': {'携程': {}}}, '浦东': {}}, '山东': {}}
import pickle,json menu_dump = json.dumps(menu) print(menu_dump) menu_dump_load = json.loads(menu_dump) print(menu_dump_load) with open('menu_dump.pkl','w') as file_1: json.dump(menu,file_1) with open('menu_dump.pkl','r') as file_2: content = json.load(file_2) print(content) #{"u5317u4eac": {"u6d77u6dc0": {"u4e94u9053u53e3": {"soho": {}, "u7f51u6613": {}, "google": {}}, "u4e2du5173u6751": {"u7231u5947u827a": {}, "u6c7du8f66u4e4bu5bb6": {}, "youku": {}}, "u4e0au5730": {"u767eu5ea6": {}}}, "u660cu5e73": {"u6c99u6cb3": {"u8001u7537u5b69": {}, "u5317u822a": {}}, "u5929u901au82d1": {}, "u56deu9f99u89c2": {}}, "u671du9633": {}, "u4e1cu57ce": {}}, "u4e0au6d77": {"u95f5u884c": {"u4ebau6c11u5e7fu573a": {"u70b8u9e21u5e97": {}}}, "u95f8u5317": {"u706bu8f66u7ad9": {"u643au7a0b": {}}}, "u6d66u4e1c": {}}, "u5c71u4e1c": {}} #{'北京': {'海淀': {'五道口': {'soho': {}, '网易': {}, 'google': {}}, '中关村': {'爱奇艺': {}, '汽车之家': {}, 'youku': {}}, '上地': {'百度': {}}}, '昌平': {'沙河': {'老男孩': {}, '北航': {}}, '天通苑': {}, '回龙观': {}}, '朝阳': {}, '东城': {}}, '上海': {'闵行': {'人民广场': {'炸鸡店': {}}}, '闸北': {'火车站': {'携程': {}}}, '浦东': {}}, '山东': {}} #{'北京': {'海淀': {'五道口': {'soho': {}, '网易': {}, 'google': {}}, '中关村': {'爱奇艺': {}, '汽车之家': {}, 'youku': {}}, '上地': {'百度': {}}}, '昌平': {'沙河': {'老男孩': {}, '北航': {}}, '天通苑': {}, '回龙观': {}}, '朝阳': {}, '东城': {}}, '上海': {'闵行': {'人民广场': {'炸鸡店': {}}}, '闸北': {'火车站': {'携程': {}}}, '浦东': {}}, '山东': {}}
# -*- encoding: utf-8 -*- import shelve f = shelve.open('shelve_test') names = ['alex','rain','test'] info = {'name':'alex','age':22} f['names'] = names f['info_dict'] = info print(f.get('names')) print(f.get('info_dict')) f.close() #['alex', 'rain', 'test'] #{'name': 'alex', 'age': 22}