⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 95.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -&gt; Persistence</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="89.html" class="navtitle">9. Input and Output</a> &gt; <span class="nonavtitle">Persistence</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="94.html" title="The print Statement"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=95" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="95.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="96.html" title="Unicode I/O"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A03%3A19+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105085094125061049248110</font><a href="read2.asp?bookname=0735710910&snode=95&now=5%2F28%2F2002+9%3A03%3A19+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Persistence</h3>
<p>It抯 often necessary to save and restore the contents of an object to a file. One approach to this problem is to write a pair of functions that read and write data from a file in a special format. An alternative approach is to use the <tT CLAss="monofont">pickle</tt>  and <TT CLass="monofont">shelve</tT>  modules.</P>

<P>The <Tt class="monofont">pickle</tt>  module serializes an object into a stream of bytes that can be written to a file. For example, the following code writes an object to a file:</p>

<pre>

import pickle 
object = someObject() 
f = open(filename,'w') 
pickle.dump(object, f)      # Save object </pre>

<p>To restore the object, you can use the following code:</p>

<pre>

import pickle 
f = open(filename,'r') 
object = pickle.load(f)   # Restore the object </Pre>

<P>The <tt cLass="monofont">shelve</tT>  module is similar, but saves objects in a dictionary-like database:</p>

<prE>

import shelve 
object = someObject() 
dbase = shelve.open(filename)    # Open a database 
dbase['key'] = object            # Save object in database 
... 
object = dbase['key']            # Retrieve it 
dbase.close()                    # Close the database </PRE>

<p>In both cases, only serializable objects can be saved to a file. Most Python objects can be serialized, but special-purpose objects such as files maintain an internal state that cannot be saved and restored in this manner. For more details about the <tt cLASS="monofont">pickle</tt>  and <tt CLASs="monofont">shelve</tt>  modules, see <a HREF="105.html">Appendix A</a>, 揟he Python Library.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -