|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 想要将函数的复杂结构序列化,就要用pickle
# 如果在另一个程序中提取数据,那么需要再定义相同的函数def sayhi(name)
# 而函数的内容可以不同
- import pickle
- # 使用pickle模块将数据对象保存到文件
- data1 = {'a': [1, 2.0, 3, 4+6j],
- 'b': ('string', u'Unicode string'),
- 'c': None}
- selfref_list = [1, 2, 3]
- selfref_list.append(selfref_list)
- print(selfref_list)
- output = open('data.pkl', 'wb')
- # Pickle dictionary using protocol 0.
- pickle.dump(data1, output)
- # Pickle the list using the highest protocol available.
- pickle.dump(selfref_list, output, -1) # 没明白:运行结果显示没产生作用
- output.close()
- pkl_file = open('data.pkl', 'rb')
- print(pickle.load(pkl_file))
- pkl_file.close()
复制代码 |
|