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

📄 node9.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
the input string is too long, they don't truncate it, but return it
unchanged; this will mess up your column lay-out but that's usually
better than the alternative, which would be lying about a value.  (If
you really want truncation you can always add a slice operation, as in
"<tt class="samp">x.ljust(&nbsp;n)[:n]</tt>".)<P>以上是一个 <tt class="method">rjust()</tt> 函数的演示,这个函数把字符串输出到一列,并通过向左侧填充空格来使其右对齐。类似的函数还有 <tt class="method">ljust()</tt> 和 <tt class="method">center()</tt>。这些函数只是输出新的字符串,并不改变什么。如果输出的字符串太长,它们也不会截断它,而是原样输出,这会使你的输出格式变得混乱,不过总强过另一种选择(截断字符串),因为那样会产生错误的输出值。(如果你确实需要截断它,可以使用切片操作,例如:" "<tt class="samp">x.ljust(&nbsp;n)[:n]</tt>"。)<P>There is another method, <tt class="method">zfill()</tt>, which pads a
numeric string on the left with zeros.  It understands about plus and
minus signs:<P>还有一个函数, <tt class="method">zfill()</tt> 它用于向数值的字符串表达左侧填充0。该函数可以正确理解正负号:<P><div class="verbatim"><pre>
&gt;&gt;&gt; '12'.zfill(5)
'00012'
&gt;&gt;&gt; '-3.14'.zfill(7)
'-003.14'
&gt;&gt;&gt; '3.14159265359'.zfill(5)
'3.14159265359'</pre></div><P>Using the <code>%</code> operator looks like this:<P>可以如下这样使用 <code>%</code> 操作符:<P><div class="verbatim"><pre>
&gt;&gt;&gt; import math
&gt;&gt;&gt; print 'The value of PI is approximately %5.3f.' % math.pi
The value of PI is approximately 3.142.</pre></div><P>If there is more than one format in the string, you need to pass a
tuple as right operand, as in this example:<P>如果有超过一个的字符串要格式化为一体,就需要将它们传入一个元组做为右值,如下所示:<P><div class="verbatim"><pre>
&gt;&gt;&gt; table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
&gt;&gt;&gt; for name, phone in table.items():
...     print '%-10s ==&gt; %10d' % (name, phone)
...
Jack       ==&gt;       4098
Dcab       ==&gt;       7678
Sjoerd     ==&gt;       4127</pre></div><P>Most formats work exactly as in C and require that you pass the proper
type; however, if you don't you get an exception, not a core dump.
The <code>%s</code> format is more relaxed: if the corresponding argument is
not a string object, it is converted to string using the
<tt class="function">str()</tt> built-in function.  Using <code>*</code> to pass the width
or precision in as a separate (integer) argument is supported.  The
C formats <code>%n</code> and <code>%p</code> are not supported.<P>大多数类 C 的格式化操作都需要你传入适当的类型,不过如果你没有定义异常,也不会有什么从内核中主动的弹出来。(however, if you don't you get an exception, not a core dump)使用 <code>%s</code> 格式会更轻松些:如果对应的参数不是字符串,它会通过内置的 <tt class="function">str()</tt> 函数转化为字符串。Python支持用 * 作为一个隔离(整型的)参数来传递宽度或精度。Python 不支持  C的 <code>%n</code> 和 <code>%p</code> 操作符。<P>If you have a really long format string that you don't want to split
up, it would be nice if you could reference the variables to be
formatted by name instead of by position.  This can be done by using
form <code>%(name)format</code>, as shown here:<P>如果可以逐点引用要格式化的变量名,就可以产生符合真实长度的格式化字符串,不会产生间隔。这一效果可以通过使用form <code>%(name)format</code> 结构来实现:<P><div class="verbatim"><pre>
&gt;&gt;&gt; table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
&gt;&gt;&gt; print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
Jack: 4098; Sjoerd: 4127; Dcab: 8637678</pre></div><P>This is particularly useful in combination with the new built-in
<tt class="function">vars()</tt> function, which returns a dictionary containing all
local variables.<P>这个技巧在与新的内置函数 <tt class="function">vars()</tt> 组合使用时非常有用,该函数返回一个包含所有局部变量的字典。<P><H1><A NAME="SECTION009200000000000000000"></A><A NAME="files"></A><BR>7.2 读写文件 Reading and Writing Files </H1><P><tt class="function">open()</tt><a id='l2h-45' xml:id='l2h-45'></a> returns a file
object<a id='l2h-46' xml:id='l2h-46'></a>, and is most commonly used with two arguments:
"<tt class="samp">open(<var>filename</var>, <var>mode</var>)</tt>".<P><tt class="function">open()</tt><a id='l2h-47' xml:id='l2h-47'></a> 返回一个文件<a id='l2h-48' xml:id='l2h-48'></a>,通常的用法需要两个参数: "<tt class="samp">open(<var>filename</var>, <var>mode</var>)</tt>"。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f=open('/tmp/workfile', 'w')
&gt;&gt;&gt; print f
&lt;open file '/tmp/workfile', mode 'w' at 80a0960&gt;</pre></div><P>The first argument is a string containing the filename.  The second
argument is another string containing a few characters describing the
way in which the file will be used.  <var>mode</var> can be <code>'r'</code> when
the file will only be read, <code>'w'</code> for only writing (an existing
file with the same name will be erased), and <code>'a'</code> opens the file
for appending; any data written to the file is automatically added to
the end.  <code>'r+'</code> opens the file for both reading and writing.
The <var>mode</var> argument is optional; <code>'r'</code> will be assumed if
it's omitted.<P>第一个参数是一个标识文件名的字符串。第二个参数是由有限的字母组成的字符串,描述了文件将会被如何使用。可选的<var>模式</var> 有: <code>'r'</code> ,此选项使文件只读; <code>'w'</code>,此选项使文件只写(对于同名文件,该操作使原有文件被覆盖); <code>'a'</code> ,此选项以追加方式打开文件; <code>'r+'</code> ,此选项以读写方式打开文件;如果没有指定,默认为 <code>'r'</code> 模式。<P>On Windows and the Macintosh, <code>'b'</code> appended to the
mode opens the file in binary mode, so there are also modes like
<code>'rb'</code>, <code>'wb'</code>, and <code>'r+b'</code>.  Windows makes a
distinction between text and binary files; the end-of-line characters
in text files are automatically altered slightly when data is read or
written.  This behind-the-scenes modification to file data is fine for
ASCII text files, but it'll corrupt binary data like that in JPEGs or
<span class="file">.EXE</span> files.  Be very careful to use binary mode when reading and
writing such files.  (Note that the precise semantics of text mode on
the Macintosh depends on the underlying C library being used.)<P>在Windows 和 Macintosh平台上, <code>'b'</code>模式以二进制方式打开文件,所以可能会有类似于 <code>'rb'</code> ,<code>'wb'</code> , <code>'r+b'</code> 等等模式组合。Windows平台上文本文件与二进制文件是有区别的,读写文本文件时,行尾会自动添加行结束符。这种后台操作方式对ASCII 文本文件没有什么问题,但是操作 JPEG 或 <span class="file">.EXE</span>这样的二进制文件时就会产生破坏。在操作这些文件时一定要记得以二进制模式打开。(需要注意的是Mactiontosh 平台上的文本模式依赖于其使用的底层C库)。<P><H2><A NAME="SECTION009210000000000000000"></A><A NAME="fileMethods"></A><BR>7.2.1 文件对象(file object)的方法 Methods of File Objects </H2><P>The rest of the examples in this section will assume that a file
object called <code>f</code> has already been created.<P>本节中的示例都默认文件对象 <code>f</code> 已经创建。<P>To read a file's contents, call <code>f.read(<var>size</var>)</code>, which reads
some quantity of data and returns it as a string.  <var>size</var> is an
optional numeric argument.  When <var>size</var> is omitted or negative,
the entire contents of the file will be read and returned; it's your
problem if the file is twice as large as your machine's memory.
Otherwise, at most <var>size</var> bytes are read and returned.  If the end
of the file has been reached, <code>f.read()</code> will return an empty
string (<code>""</code>).<P>要读取文件内容,需要调用 <code>f.read(<var>size</var>)</code>,该方法读取若干数量的数据并以字符串形式返回其内容,字符串长度为数值<var>size</var> 所指定的大小。如果没有指定 <var>size</var>或者指定为负数,就会读取并返回整个文件。当文件大小为当前机器内存两倍时,就会产生问题。正常情况下,会尽可能按比较大的<var>size</var> 读取和返回数据。如果到了文件末尾,<code>f.read()</code>会返回一个空字符串(<code>""</code>)。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f.read()
'This is the entire file.\n'
&gt;&gt;&gt; f.read()
''</pre></div><P><code>f.readline()</code> reads a single line from the file; a newline
character (<code>&#92;n</code>) is left at the end of the string, and is only
omitted on the last line of the file if the file doesn't end in a
newline.  This makes the return value unambiguous; if
<code>f.readline()</code> returns an empty string, the end of the file has
been reached, while a blank line is represented by <code>'&#92;n'</code>, a
string containing only a single newline.<P><code>f.readline()</code>从文件中读取单独一行,字符串结尾会自动加上一个换行符,只有当文件最后一行没有以换行符结尾时,这一操作才会被忽略。这样返回值就不会有什么混淆不清,如果如果 <code>f.readline()</code>返回一个空字符串,那就表示到达了文件末尾,如果是一个空行,就会描述为<code>'&#92;n&#180;</code> ,一个只包含换行符的字符串。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f.readline()
'This is the first line of the file.\n'
&gt;&gt;&gt; f.readline()
'Second line of the file\n'
&gt;&gt;&gt; f.readline()
''</pre></div><P><code>f.readlines()</code> returns a list containing all the lines of data
in the file.  If given an optional parameter <var>sizehint</var>, it reads
that many bytes from the file and enough more to complete a line, and
returns the lines from that.  This is often used to allow efficient
reading of a large file by lines, but without having to load the
entire file in memory.  Only complete lines will be returned.<P><code>f.readlines()</code>返回一个列表,其中包含了文件中所有的数据行。如果给定了<var>sizehint</var>参数,就会读入多于一行的比特数,从中返回多行文本。这个功能通常用于高效读取大型行文件,避免了将整个文件读入内存。这种操作只返回完整的行。<P><div class="verbatim"><pre>
&gt;&gt;&gt; f.readlines()
['This is the first line of the file.\n', 'Second line of the file\n']</pre></div><P><code>f.write(<var>string</var>)</code> writes the contents of <var>string</var> to

⌨️ 快捷键说明

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