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

📄 node9.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
the file, returning <code>None</code>.<P><code>f.write(<var>string</var>)</code> 将 <var>string</var> 的内容写入文件,返回
<code>None</code> 。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f.write('This is a test\n')</pre></div><P>To write something other than a string, it needs to be converted to a
string first:<P>如果需要写入字符串以外的数据,就要先把这些数据转换为字符串。<P><div class="verbatim"><pre>
&gt;&gt;&gt; value = ('the answer', 42)
&gt;&gt;&gt; s = str(value)
&gt;&gt;&gt; f.write(s)</pre></div><P><code>f.tell()</code> returns an integer giving the file object's current
position in the file, measured in bytes from the beginning of the
file.  To change the file object's position, use
"<tt class="samp">f.seek(<var>offset</var>, <var>from_what</var>)</tt>".  The position is
computed from adding <var>offset</var> to a reference point; the reference
point is selected by the <var>from_what</var> argument.  A
<var>from_what</var> value of 0 measures from the beginning of the file, 1
uses the current file position, and 2 uses the end of the file as the
reference point.  <var>from_what</var> can be omitted and defaults to 0,
using the beginning of the file as the reference point.<P><code>f.tell()</code>返回一个整数,代表文件对象在文件中的指针位置,该数值计量了自文件开头到指针处的比特数。需要改变文件对象指针话话,使用"<tt class="samp">f.seek(<var>offset</var>,<var>from_what</var>)</tt>" 。指针在该操作中从指定的引用位置移动<var>offset</var> 比特,引用位置由 <var>from_what</var> 参数指定。 <var>from_what</var>值为0表示自文件起初处开始,1表示自当前文件指针位置开始,2表示自文件末尾开始。 <var>from_what</var> 可以忽略,其默认值为零,此时从文件头开始。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f = open('/tmp/workfile', 'r+')
&gt;&gt;&gt; f.write('0123456789abcdef')
&gt;&gt;&gt; f.seek(5)     # Go to the 6th byte in the file
&gt;&gt;&gt; f.read(1)
'5'
&gt;&gt;&gt; f.seek(-3, 2) # Go to the 3rd byte before the end
&gt;&gt;&gt; f.read(1)
'd'</pre></div><P>When you're done with a file, call <code>f.close()</code> to close it and
free up any system resources taken up by the open file.  After calling
<code>f.close()</code>, attempts to use the file object will automatically fail.<P>文件使用完后,调用 <code>f.close()</code>可以关闭文件,释放打开文件后占用的系统资源。调用 <code>f.close()</code>之后,再调用文件对象会自动引发错误。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f.close()
&gt;&gt;&gt; f.read()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
ValueError: I/O operation on closed file</pre></div><P>File objects have some additional methods, such as
<tt class="method">isatty()</tt> and <tt class="method">truncate()</tt> which are less frequently
used; consult the Library Reference for a complete guide to file
objects.<P>文件对象还有一些不太常用的附加方法,比如 <tt class="method">isatty()</tt> 和<tt class="method">truncate()</tt> 在库参考手册中有文件对象的完整指南。<P><H2><A NAME="SECTION009220000000000000000"></A><A NAME="pickle"></A><BR>7.2.2 <tt class="module">pickle</tt> 模块 <tt class="module">pickle</tt> Module </H2>
<a id='l2h-49' xml:id='l2h-49'></a><P>Strings can easily be written to and read from a file. Numbers take a
bit more effort, since the <tt class="method">read()</tt> method only returns
strings, which will have to be passed to a function like
<tt class="function">int()</tt>, which takes a string like <code>'123'</code> and
returns its numeric value 123.  However, when you want to save more
complex data types like lists, dictionaries, or class instances,
things get a lot more complicated.<P>我们可以很容易的读写文件中的字符串。数值就要多费点儿周折,因为<tt class="method">read()</tt> 方法只会返回字符串,应该将其传入 <tt class="function">int()</tt>方法中,就可以将 <code>'123'</code>这样的字符转为对应的数值123。不过,当你需要保存更为复杂的数据类型,例如链表、字典,类的实例,事情就会变得更复杂了。<P>Rather than have users be constantly writing and debugging code to
save complicated data types, Python provides a standard module called
<a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a>.  This is an
amazing module that can take almost
any Python object (even some forms of Python code!), and convert it to
a string representation; this process is called <i class="dfn">pickling</i>.
Reconstructing the object from the string representation is called
<i class="dfn">unpickling</i>.  Between pickling and unpickling, the string
representing the object may have been stored in a file or data, or
sent over a network connection to some distant machine.<P>好在用户不必要非得自己编写和调试保存复杂数据类型的代码。 Python提供了一个名为 <a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a>的标准模块。这是一个令人赞叹的模块,几乎可以把任何 Python对象 (甚至是一些 Python 代码段!)表达为为字符串,这一过程称之为<i class="dfn">封装</i> ( <i class="dfn">pickling</i>)。从字符串表达出重新构造对象称之为<i class="dfn">拆封</i>( <i class="dfn">unpickling</i>)。封装状态中的对象可以存储在文件或对象中,也可以通过网络在远程的机器之间传输。<P>If you have an object <code>x</code>, and a file object <code>f</code> that's been
opened for writing, the simplest way to pickle the object takes only
one line of code:<P>如果你有一个对象 <code>x</code> ,一个以写模式打开的文件对象 <code>f</code>,封装对像的最简单的方法只需要一行代码:<P><div class="verbatim"><pre>
pickle.dump(x, f)</pre></div><P>To unpickle the object again, if <code>f</code> is a file object which has
been opened for reading:<P>如果 <code>f</code>是一个以读模式打开的文件对象,就可以重装拆封这个对象:<P><div class="verbatim"><pre>
x = pickle.load(f)</pre></div><P>(There are other variants of this, used when pickling many objects or
when you don't want to write the pickled data to a file; consult the
complete documentation for
<a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a> in the
<em class="citetitle"><a href="../lib/" title="Python Library Reference" >Python Library Reference</a></em>.)<P>(如果不想把封装的数据写入文件,这里还有一些其它的变化可用。完整的<a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a>
文档请见<em class="citetitle"><a href="../lib/" title="Python 库参考手册" >Python 库参考手册</a></em>)。<P><a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a> is the standard way
to make Python objects which can be stored and reused by other
programs or by a future invocation of the same program; the technical
term for this is a <i class="dfn">persistent</i> object.  Because
<a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a> is so widely used,
many authors who write Python extensions take care to ensure that new
data types such as matrices can be properly pickled and unpickled.<P><a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a> 是存储 Python 对象以供其它程序或其本身以后调用的标准方法。提供这一组技术的是一个持久化对象( <i class="dfn">persistent</i> object )。因为 <a class="ulink" href="../lib/module-pickle.html"  ><tt class="module">pickle</tt></a> 的用途很广泛,很多 Python 扩展的作者都非常注意类似矩阵这样的新数据类型是否适合封装和拆封。<P><DIV CLASS="navigation"><div class='online-navigation'><p></p><hr /><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="6. 模块 Modules"  href="node8.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="8. 错误和异常 Errors and"  href="node10.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node8.html">6. 模块 Modules</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node10.html">8. 错误和异常 Errors and</A></div></div><hr /></DIV><!--End of Navigation Panel--><ADDRESS>译者:刘鑫(march.liu AT gmail DOT com) 由:limodou转(limodou AT gmail DOT com)<br>See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

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