📄 ch12s02.html#
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--@import url(stylesheet/text.css);@import url(stylesheet/box.css);--></style><title>简明 Python 教程 / 输入/输出 / 储存器 </title></head><body><table width="100%"><tr><th colspan="3" align="center"><span class="header">简明 Python 教程</span></th></tr><th colspan="3" align="center">第12章 输入/输出</th><tr><th width="20%" align="left"><a href="ch12.html#s01">上一页</a></th><th width="60%" align="center"><span class="header2">储存器</span></th><th align="right"><a href="ch12s03.html">下一页</a></th></tr></table><hr noshade><h1>储存器</h1><p>Python提供一个标准的模块,称为<code>pickle</code>。使用它你可以在一个文件中储存<strong>任何</strong>Python对象,之后你又可以把它完整无缺地取出来。这被称为 <dfn>持久地</dfn> 储存对象。</p><p>还有另一个模块称为<code>cPickle</code>,它的功能和<code>pickle</code>模块完全相同,只不过它是用C语言编写的,因此要快得多(比<code>pickle</code>快1000倍)。你可以使用它们中的任一个,而我们在这里将使用<code>cPickle</code>模块。记住,我们把这两个模块都简称为<code>pickle</code>模块。</p><h2><a name="pickling">储存与取储存</a></h2><p class="exampletitle"><a name="e122">例12.2 储存与取储存</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: pickling.py</code><br><br><code class="key">import </code><code class="func">cPickle </code><code>as p</code><br><code class="comment">#import pickle as p</code><br><br><code>shoplistfile = </code><code class="cite">'shoplist.data'</code><br><code class="comment"># the name of the file where we will store the object</code><br><br><code>shoplist = [</code><code class="cite">'apple'</code><code>, </code><code class="cite">'mango'</code><code>, </code><code class="cite">'carrot'</code><code>]</code><br><br><code class="comment"># Write to the file</code><br><code>f = </code><code class="func">file</code><code>(shoplistfile, </code><code class="cite">'w'</code><code>)<br>p.dump(shoplist, f) </code><code class="comment"># dump the object to a file</code><br><code>f.close()</code><br><br><code class="key">del </code><code>shoplist </code><code class="comment"># remove the shoplist<br><br># Read back from the storage</code><br><code>f = </code><code class="func">file</code><code>(shoplistfile)<br>storedlist = p.load(f)</code><br><code class="key">print </code><code>storedlist</code></p><p>(源文件:<a href="code/pickling.py">code/pickling.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python pickling.py<br>['apple', 'mango', 'carrot']</code></p><h2>它如何工作</h2><p>首先,请注意我们使用了<code>import..as</code>语法。这是一种便利方法,以便于我们可以使用更短的模块名称。在这个例子中,它还让我们能够通过简单地改变一行就切换到另一个模块(<code>cPickle</code>或者<code>pickle</code>)!在程序的其余部分的时候,我们简单地把这个模块称为<code>p</code>。</p><p>为了在文件里储存一个对象,首先以写模式打开一个<code>file</code>对象,然后调用储存器模块的<code>dump</code>函数,把对象储存到打开的文件中。这个过程称为 <dfn>储存</dfn> 。</p><p>接下来,我们使用<code>pickle</code>模块的<code>load</code>函数的返回来取回对象。这个过程称为 <dfn>取储存</dfn> 。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch12.html#s01">上一页</a></th><th width="60%" align="center"><a href="ch12.html">上一级</a></th><th width="20%" align="right"><a href="ch12s03.html">下一页</a></th></tr><tr><th width="20%" align="left">文件</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">概括</th></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -