A “shelf” is a persistent, dictionary-like object. The difference with
“dbm” databases is that the values (not the keys!) in a shelf can be
essentially arbitrary Python objects — anything that the pickle module
can handle. This includes most class instances, recursive data types,
and objects containing lots of shared sub-objects. The keys are
ordinary strings.
你好,J Nowak
我认为您要做的是将输入数据保存到一个文件(例如txt文件)。 您可以查看下面的链接,它将向您展示如何读写文本文件。你知道吗
How to read and write to text file in Python
有许多方法可以使数据持久化。 这取决于任务、环境等。 举几个例子:
最简单/基本的方法是使用文件。 甚至还有一些模块允许透明地执行此操作。 你只是一如既往地处理你的数据。你知道吗
例如参见
shelve
。你知道吗根据文件:
用法示例:
你正常地使用
s
,因为它只是一个普通的字典。 它会自动保存在磁盘上(在本例中是文件test_shelf.db
)。你知道吗在这种情况下,你的字典是持久的 并且在程序重新启动后不会丢失其值。你知道吗
更多信息:
另一个选择是使用pickle,它可以提供持久性 同样,但不是神奇的:你需要自己读写数据。你知道吗
比较
shelve
和pickle
:相关问题 更多 >
编程相关推荐