394 lines
32 KiB
HTML
394 lines
32 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>shelve --- Python 对象持久化 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="marshal --- 内部 Python 对象序列化" href="marshal.html" />
|
||
<link rel="prev" title="copyreg --- 注册配合 pickle 模块使用的函数" href="copyreg.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/shelve.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> --- Python 对象持久化</a><ul>
|
||
<li><a class="reference internal" href="#restrictions">限制</a></li>
|
||
<li><a class="reference internal" href="#example">示例</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="copyreg.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code> --- 注册配合 <code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> 模块使用的函数</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="marshal.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> --- 内部 Python 对象序列化</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/shelve.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/shelve.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="marshal.html" title="marshal --- 内部 Python 对象序列化"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="copyreg.html" title="copyreg --- 注册配合 pickle 模块使用的函数"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="persistence.html" accesskey="U">数据持久化</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-shelve">
|
||
<span id="shelve-python-object-persistence"></span><h1><a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> --- Python 对象持久化<a class="headerlink" href="#module-shelve" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/shelve.py">Lib/shelve.py</a></p>
|
||
<hr class="docutils" id="index-0" />
|
||
<p>"Shelf" 是一种持久化的类似字典的对象。 与 "dbm" 数据库的区别在于 Shelf 中的值(不是键!)实际上可以为任意 Python 对象 --- 即 <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> 模块能够处理的任何东西。 这包括大部分类实例、递归数据类型,以及包含大量共享子对象的对象。 键则为普通的字符串。</p>
|
||
<dl class="function">
|
||
<dt id="shelve.open">
|
||
<code class="sig-prename descclassname">shelve.</code><code class="sig-name descname">open</code><span class="sig-paren">(</span><em class="sig-param">filename</em>, <em class="sig-param">flag='c'</em>, <em class="sig-param">protocol=None</em>, <em class="sig-param">writeback=False</em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.open" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>打开一个持久化字典。 filename 指定下层数据库的基准文件名。 作为附带效果,会为 filename 添加一个扩展名并且可能创建更多的文件。 默认情况下,下层数据库会以读写模式打开。 可选的 <em>flag</em> 形参具有与 <a class="reference internal" href="dbm.html#dbm.open" title="dbm.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">dbm.open()</span></code></a> <em>flag</em> 形参相同的含义。</p>
|
||
<p>默认会使用第 3 版 pickle 协议来序列化值。 pickle 协议版本可通过 <em>protocol</em> 形参来指定。</p>
|
||
<p>由于 Python 语义的限制,Shelf 对象无法确定一个可变的持久化字典条目在何时被修改。 默认情况下 <em>只有</em> 在被修改对象再赋值给 shelf 时才会写入该对象 (参见 <a class="reference internal" href="#shelve-example"><span class="std std-ref">示例</span></a>)。 如果可选的 <em>writeback</em> 形参设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,则所有被访问的条目都将在内存中被缓存,并会在 <a class="reference internal" href="#shelve.Shelf.sync" title="shelve.Shelf.sync"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sync()</span></code></a> 和 <a class="reference internal" href="#shelve.Shelf.close" title="shelve.Shelf.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> 时被写入;这可以使得对持久化字典中可变条目的修改更方便,但是如果访问的条目很多,这会消耗大量内存作为缓存,并会使得关闭操作变得非常缓慢,因为所有被访问的条目都需要写回到字典(无法确定被访问的条目中哪个是可变的,也无法确定哪个被实际修改了)。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>请不要依赖于 Shelf 的自动关闭功能;当你不再需要时应当总是显式地调用 <a class="reference internal" href="#shelve.Shelf.close" title="shelve.Shelf.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>,或者使用 <a class="reference internal" href="#shelve.open" title="shelve.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">shelve.open()</span></code></a> 作为上下文管理器:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">shelve</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'spam'</span><span class="p">)</span> <span class="k">as</span> <span class="n">db</span><span class="p">:</span>
|
||
<span class="n">db</span><span class="p">[</span><span class="s1">'eggs'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'eggs'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>由于 <a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> 模块需要 <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> 的支持,因此从不可靠的来源载入 shelf 是不安全的。 与 pickle 一样,载入 Shelf 时可以执行任意代码。</p>
|
||
</div>
|
||
<p>字典所支持的所有方法都被 Shelf 对象所支持。 因此很容易将基于字典的代码转换为需要持久化存储的代码。</p>
|
||
<p>额外支持的两个方法:</p>
|
||
<dl class="method">
|
||
<dt id="shelve.Shelf.sync">
|
||
<code class="sig-prename descclassname">Shelf.</code><code class="sig-name descname">sync</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shelve.Shelf.sync" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果 Shelf 打开时将 <em>writeback</em> 设为 <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code></a> 则写回缓存中的所有条目。 如果可行还会清空缓存并将持久化字典同步到磁盘。 此方法会在使用 <a class="reference internal" href="#shelve.Shelf.close" title="shelve.Shelf.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> 关闭 Shelf 时自动被调用。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="shelve.Shelf.close">
|
||
<code class="sig-prename descclassname">Shelf.</code><code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#shelve.Shelf.close" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>同步并关闭持久化 <em>dict</em> 对象。 对已关闭 Shelf 的操作将失败并引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<p><a class="reference external" href="https://code.activestate.com/recipes/576642/">持久化字典方案</a>,使用了广泛支持的存储格式并具有原生字典的速度。</p>
|
||
</div>
|
||
<section id="restrictions">
|
||
<h2>限制<a class="headerlink" href="#restrictions" title="永久链接至标题">¶</a></h2>
|
||
<blockquote>
|
||
<div></div></blockquote>
|
||
<ul class="simple" id="index-1">
|
||
<li><p>可选择使用哪种数据库包 (例如 <a class="reference internal" href="dbm.html#module-dbm.ndbm" title="dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a> 或 <a class="reference internal" href="dbm.html#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a>) 取决于支持哪种接口。 因此使用 <a class="reference internal" href="dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> 直接打开数据库是不安全的。 如果使用了 <a class="reference internal" href="dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>,数据库同样会(不幸地)受限于它 --- 这意味着存储在数据库中的(封存形式的)对象尺寸应当较小,并且在少数情况下键冲突有可能导致数据库拒绝更新。</p></li>
|
||
<li><p><a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> 模块不支持对 Shelf 对象的 <em>并发</em> 读/写访问。 (多个同时读取访问则是安全的。) 当一个程序打开一个 shelve 对象来写入时,不应再有其他程序同时打开它来读取或写入。 Unix 文件锁定可被用来解决此问题,但这在不同 Unix 版本上会存在差异,并且需要有关所用数据库实现的细节知识。</p></li>
|
||
</ul>
|
||
<dl class="class">
|
||
<dt id="shelve.Shelf">
|
||
<em class="property">class </em><code class="sig-prename descclassname">shelve.</code><code class="sig-name descname">Shelf</code><span class="sig-paren">(</span><em class="sig-param">dict</em>, <em class="sig-param">protocol=None</em>, <em class="sig-param">writeback=False</em>, <em class="sig-param">keyencoding='utf-8'</em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.Shelf" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="collections.abc.html#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.MutableMapping</span></code></a> 的一个子类,它会将封存的值保存在 <em>dict</em> 对象中。</p>
|
||
<p>默认会使用第 3 版 pickle 协议来序列化值。 pickle 协议版本可通过 <em>protocol</em> 形参来指定。 请参阅 <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> 文档来查看 pickle 协议的相关讨论。</p>
|
||
<p>如果 <em>writeback</em> 形参为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,对象将为所有访问过的条目保留缓存并在同步和关闭时将它们写回到 <em>dict</em>。 这允许对可变的条目执行自然操作,但是会消耗更多内存并让同步和关闭花费更长时间。</p>
|
||
<p><em>keyencoding</em> 形参是在下层字典被使用之前用于编码键的编码格式。</p>
|
||
<p><a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> 对象还可以被用作上下文管理器,在这种情况下它将在 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句块结束时自动被关闭。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了 <em>keyencoding</em> 形参;之前,键总是使用 UTF-8 编码。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>添加了上下文管理器支持。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="class">
|
||
<dt id="shelve.BsdDbShelf">
|
||
<em class="property">class </em><code class="sig-prename descclassname">shelve.</code><code class="sig-name descname">BsdDbShelf</code><span class="sig-paren">(</span><em class="sig-param">dict</em>, <em class="sig-param">protocol=None</em>, <em class="sig-param">writeback=False</em>, <em class="sig-param">keyencoding='utf-8'</em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.BsdDbShelf" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> 的一个子类,将 <code class="xref py py-meth docutils literal notranslate"><span class="pre">first()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">previous()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">last()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">set_location()</span></code> 对外公开,在来自 <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">pybsddb</a> 的第三方 <code class="xref py py-mod docutils literal notranslate"><span class="pre">bsddb</span></code> 模块中可用,但在其他数据库模块中不可用。 传给构造器的 <em>dict</em> 对象必须支持这些方法。 这通常是通过调用 <code class="xref py py-func docutils literal notranslate"><span class="pre">bsddb.hashopen()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">bsddb.btopen()</span></code> 或 <code class="xref py py-func docutils literal notranslate"><span class="pre">bsddb.rnopen()</span></code> 之一来完成的。 可选的 <em>protocol</em>, <em>writeback</em> 和 <em>keyencoding</em> 形参具有与 <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> 类相同的含义。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="class">
|
||
<dt id="shelve.DbfilenameShelf">
|
||
<em class="property">class </em><code class="sig-prename descclassname">shelve.</code><code class="sig-name descname">DbfilenameShelf</code><span class="sig-paren">(</span><em class="sig-param">filename</em>, <em class="sig-param">flag='c'</em>, <em class="sig-param">protocol=None</em>, <em class="sig-param">writeback=False</em><span class="sig-paren">)</span><a class="headerlink" href="#shelve.DbfilenameShelf" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> 的一个子类,它接受一个 <em>filename</em> 而非字典类对象。 下层文件将使用 <a class="reference internal" href="dbm.html#dbm.open" title="dbm.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">dbm.open()</span></code></a> 来打开。 默认情况下,文件将以读写模式打开。 可选的 <em>flag</em> 形参具有与 <a class="reference internal" href="#shelve.open" title="shelve.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 函数相同的含义。 可选的 <em>protocol</em> 和 <em>writeback</em> 形参具有与 <a class="reference internal" href="#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a> 类相同的含义。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="example">
|
||
<span id="shelve-example"></span><h2>示例<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h2>
|
||
<p>对接口的总结如下 (<code class="docutils literal notranslate"><span class="pre">key</span></code> 为字符串,<code class="docutils literal notranslate"><span class="pre">data</span></code> 为任意对象):</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">shelve</span>
|
||
|
||
<span class="n">d</span> <span class="o">=</span> <span class="n">shelve</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="c1"># open -- file may get suffix added by low-level</span>
|
||
<span class="c1"># library</span>
|
||
|
||
<span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span> <span class="c1"># store data at key (overwrites old data if</span>
|
||
<span class="c1"># using an existing key)</span>
|
||
<span class="n">data</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="c1"># retrieve a COPY of data at key (raise KeyError</span>
|
||
<span class="c1"># if no such key)</span>
|
||
<span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="c1"># delete data stored at key (raises KeyError</span>
|
||
<span class="c1"># if no such key)</span>
|
||
|
||
<span class="n">flag</span> <span class="o">=</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span> <span class="c1"># true if the key exists</span>
|
||
<span class="n">klist</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="c1"># a list of all existing keys (slow!)</span>
|
||
|
||
<span class="c1"># as d was opened WITHOUT writeback=True, beware:</span>
|
||
<span class="n">d</span><span class="p">[</span><span class="s1">'xx'</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="c1"># this works as expected, but...</span>
|
||
<span class="n">d</span><span class="p">[</span><span class="s1">'xx'</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!</span>
|
||
|
||
<span class="c1"># having opened d without writeback=True, you need to code carefully:</span>
|
||
<span class="n">temp</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="s1">'xx'</span><span class="p">]</span> <span class="c1"># extracts the copy</span>
|
||
<span class="n">temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="c1"># mutates the copy</span>
|
||
<span class="n">d</span><span class="p">[</span><span class="s1">'xx'</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span> <span class="c1"># stores the copy right back, to persist it</span>
|
||
|
||
<span class="c1"># or, d=shelve.open(filename,writeback=True) would let you just code</span>
|
||
<span class="c1"># d['xx'].append(5) and have it work as expected, BUT it would also</span>
|
||
<span class="c1"># consume more memory and make the d.close() operation slower.</span>
|
||
|
||
<span class="n">d</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="c1"># close it</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt>模块 <a class="reference internal" href="dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a></dt><dd><p><code class="docutils literal notranslate"><span class="pre">dbm</span></code> 风格数据库的泛型接口。</p>
|
||
</dd>
|
||
<dt>模块 <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a></dt><dd><p><a class="reference internal" href="#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> 所使用的对象序列化。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code> --- Python 对象持久化</a><ul>
|
||
<li><a class="reference internal" href="#restrictions">限制</a></li>
|
||
<li><a class="reference internal" href="#example">示例</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="copyreg.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">copyreg</span></code> --- 注册配合 <code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code> 模块使用的函数</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="marshal.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code> --- 内部 Python 对象序列化</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/shelve.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="marshal.html" title="marshal --- 内部 Python 对象序列化"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="copyreg.html" title="copyreg --- 注册配合 pickle 模块使用的函数"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="persistence.html" >数据持久化</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |