pickle序列化2

# 想要将函数的复杂结构序列化,就要用pickle

# 如果在另一个程序中提取数据,那么需要再定义相同的函数def sayhi(name)

# 而函数的内容可以不同 

 1 import pickle
 2 
 3 # 使用pickle模块将数据对象保存到文件
 4 data1 = {'a': [1, 2.0, 3, 4+6j],
 5          'b': ('string', u'Unicode string'),
 6          'c': None}
 7 selfref_list = [1, 2, 3]
 8 selfref_list.append(selfref_list)
 9 print(selfref_list)
10 output = open('data.pkl', 'wb')
11 # Pickle dictionary using protocol 0.
12 pickle.dump(data1, output)
13 # Pickle the list using the highest protocol available.
14 pickle.dump(selfref_list, output, -1)  # 没明白:运行结果显示没产生作用
15 output.close()
16 pkl_file = open('data.pkl', 'rb')
17 print(pickle.load(pkl_file))
18 pkl_file.close()
原文地址:https://www.cnblogs.com/gzj137070928/p/13730200.html