Import Shelvedef createdata (): try: db = Shelve.open (' shelve_test ') # Key with value must be string db[' int '] = 1 db[' float '] = 2.3 db[' string '] = "I like python." db[' key ' = ' value ' finally: db.close () def loaddata (): #这个可以另外写一个. py file to read db = Shelve.open (' Shelve _test ') for item in Db.items (): Print ( item) Print (db[' int ')) print ( db[' float ')) print (db [' String ']) Print (db[' key ')) db.close () if __name__ = = ' __main__ ': createdata () loaddata ()
The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable
Pickle write: Open a file descriptor in write mode, call Pickle.dump to write the object in
Import Pickledn = {' Baidu ': ' www.baidu.com ', ' qq ': ' www.qq.com ', ' a ': ' www.360.cn '}name = [' Mayun ', ' Mahuateng ', ' Liyanhong ']f = open (' A.txt ', ' WB ') # ' WB ' is written in binary pickle.dump (dn,f) # #写一个对象pickle. Dump (name,f) # # Write one More object F.close () read: Open file descriptor in read mode, call Pickle.load Load Object f = open (' a.txt ', ' RB ') # ' WB ' read pickle.load (f) # # Load an object Pickle.load (f) # #加载另一个对象f. Close ()
Python shelve module