1.pickle
Write: Open a file descriptor in write mode, call Pickle.dump to write the object in
Copy the Code code as follows:
DN = {' Baidu ': ' www.baidu.com ', ' qq ': ' www.qq.com ', ' a ': ' www.360.cn '}
name = [' Mayun ', ' Mahuateng ', ' Liyanhong ']
f = open (R ' C:\a.txt ', ' W ')
Pickle.dump (dn,f) # #写一个对象
Pickle.dump (name,f) # #再写一个对象
F.close ()
READ: Open file descriptor as read, call Pickle.load Load Object
f = open (R ' C:\a.txt ')
Pickle.load (f) # #加载一个对象
Pickle.load (f) # #加载另一个对象
F.close ()
2.shelve
The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable
Copy the Code code as follows:
F = Shelve.open (R ' C:\b.txt ')
F # #返回空字典
f[' baidu '] = ' www.baidu.com '
f[' qq '] = ' www.qq.com '
f[' www.360.cn ') = '
F # #返回字典
F.close ()
F = Shelve.open (R ' C:\b.txt ')
F # #返回字典f