【发布时间】:2018-12-03 10:07:21
【问题描述】:
如何在程序中保存值,以便“记住”它们?
【问题讨论】:
标签: python
如何在程序中保存值,以便“记住”它们?
【问题讨论】:
标签: python
这是直接来自文档的示例,
import pickle
# An arbitrary collection of objects supported by pickle.
data = {
'a': [1, 2.0, 3, 4+6j],
'b': ("character string", b"byte string"),
'c': {None, True, False}
}
with open('data.pickle', 'wb') as f:
# Pickle the 'data' dictionary using the highest protocol available.
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
import pickle
with open('data.pickle', 'rb') as f:
# The protocol version used is detected automatically, so we do not
# have to specify it.
data = pickle.load(f)
【讨论】: