python的序列化

这篇只是笔记而已,用于记录python编程中那些比较好的做法.

python中经常使用的序列化模块是pickle,下面是一个例子:

import pickle
if __name__ == '__main__':    
  dic = dict()   
  dic[1] = 2    
  dic[2] = 3    
  lis = ["hi", "girls"]    
  with open('hi.dump', 'wb') as f:
       pickle.dump(dic, f) 
       pickle.dump(lis, f)
  with open('hi.dump', 'rb') as f:
        d1 = pickle.load(f)       
 d2 = pickle.load(f)   
 print(d1)   
 print(d2)

输出的结果是:

{1: 2, 2: 3}
['hi', 'girls']

你可能感兴趣的:(python的序列化)