📄 node9.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head> <title>7. 输入(Input)与输出(Output)</title> <meta name="description" content="7. 输入(Input)与输出(Output) "> <meta name="keywords" content="tut"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <link rel="STYLESHEET" href="tut.css"> <link rel="next" href="node10.html"> <link rel="previous" href="node8.html"> <link rel="up" href="tut.html"> <link rel="next" href="node10.html"></head> <body> <div class="navigation"><table align="Center" width="100%" cellpadding="0" cellspacing="2"> <tbody> <tr> <td><a href="node8.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></a></td> <td><a href="tut.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></a></td> <td><a href="node10.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></a></td> <td align="Center" width="100%">Python 教学文件</td> <td><a href="node2.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></a></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> </tr> </tbody></table> <b class="navlabel">Previous:</b> <a class="sectref" href="node8.html">6.模组</a> <b class="navlabel">Up:</b> <a class="sectref" href="tut.html">Python教学文件</a> <b class="navlabel">Next:</b> <a class="sectref" href="node10.html">8. 程式错误与例外(Exceptions)情形</a> <br><hr></div> <!--End of Navigation Panel--> <!--Table of Child-Links--> <a name="CHILD_LINKS"><strong>小段落 </strong></a> <ul> <li><a name="tex2html324" href="node9.html#SECTION009100000000000000000">7.1 花俏的输出格式化 </a> </li> <li><a name="tex2html325" href="node9.html#SECTION009200000000000000000">7.2 读写档案 </a> <ul> <li><a name="tex2html326" href="node9.html#SECTION009210000000000000000">7.2.1 File物件的Methods(方法) </a> </li> <li><a name="tex2html327" href="node9.html#SECTION009220000000000000000">7.2.2 <tt class="module">pickle</tt> 模组 </a> </li> </ul> </li></ul> <!--End of Table of Child-Links--> <hr> <h1> <br> 7. 输入(Input)与输出(Output) </h1> <p> 有很多的方式可以来表现一个程式的输出结果,可以印出来在一个可读的表格里面,也可以写入到档案里面供作未来使用。这一章里面将谈到一些可能的方法。 </p><p> </p><h1> <br> 7.1 花俏的输出格式化 </h1> <p> 到现在为止我们谈到了两种写入值的方式:用expression的叙述( <i>expression statements</i> ),或是用<tt class="keyword">print</tt> 这个叙述。 (第三种方法是使用file物件的 <tt class="method">write()</tt> 方法(method),这一个标准输出所指向的档案(standard output file),可以用 <code>sys.stdout</code> 来存取之。请参阅程式库参考手册上面对此的详细说明。) </p><p> 通常你会希望你对于输出的结果能够在格式上面稍有控制力,而不只是预设的用空白连结起来而已。有两种方法可以来控制输出的格式,第一种是自己动手来做字串的调整。你可以使用字串的切割(slicing)以及连结,做成任何你想要的效果。标准的 <tt class="module">string</tt> module里面有一些好用的东西,也可以帮助你填入适当的空白,使字串的宽度成为你想要的宽度,我们待会再来讨论如何做。另外一个控制输出格式的方法是使用<code>%</code> 这个运算元,配合上用字串成为左边的参数。这个运算元会翻译左边的这个字串参数,其功能类似于C里面的 <tt class="cfunction">sprintf()</tt> 的字串参数,然后把右边要控制的字串适当的填入,之后再传回这个格式化的结果。 </p><p> 还有一个问题,如何把其他的值转换成洽当的字串呢?幸好Python里面的 <tt class="function">repr()</tt>函式可以转换任何的值成为一个字串,你以可以把这个值写在反撇号( <code>` `</code> )的中间也有同样的效果。请看一些例子: </p><p> </p><dl><dd><pre class="verbatim">>>> x = 10 * 3.14<br>>>> y = 200*200<br>>>> s = 'The value of x is ' + `x` + ', and y is ' + `y` + '...'<br>>>> print s<br>The value of x is 31.4, and y is 40000...<br>>>> # Reverse quotes work on other types besides numbers:<br>... p = [x, y]<br>>>> ps = repr(p)<br>>>> ps<br>'[31.4, 40000]'<br>>>> # Converting a string adds string quotes and backslashes:<br>... hello = 'hello, world\n'<br>>>> hellos = `hello`<br>>>> print hellos<br>'hello, world\012'<br>>>> # The argument of reverse quotes may be a tuple:<br>... `x, y, ('spam', 'eggs')`<br>"(31.4, 40000, ('spam', 'eggs'))"<br></pre> </dd> </dl> <p> 底下我们示范两种格式化的方法,这例子是写入平方及立方值: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> import string<br>>>> for x in range(1, 11):<br>... print string.rjust(`x`, 2), string.rjust(`x*x`, 3),<br>... # Note trailing comma on previous line<br>... print string.rjust(`x*x*x`, 4)<br>...<br> 1 1 1<br> 2 4 8<br> 3 9 27<br> 4 16 64<br> 5 25 125<br> 6 36 216<br> 7 49 343<br> 8 64 512<br> 9 81 729<br>10 100 1000<br>>>> for x in range(1,11):<br>... print '%2d %3d %4d' % (x, x*x, x*x*x)<br>... <br> 1 1 1<br> 2 4 8<br> 3 9 27<br> 4 16 64<br> 5 25 125<br> 6 36 216<br> 7 49 343<br> 8 64 512<br> 9 81 729<br>10 100 1000<br></pre> </dd> </dl> <p> (值得注意的是在数目字中间的空白是使用 <tt class="keyword">print</tt> 的结果,<tt class="keyword">print</tt> 总是会在每一个参数中间加入空白。) </p> <p> 这个例子示范了使用 <tt class="function">string.rjust()</tt> 的方法,这个函式会使的一个字串在指定的宽度里左边加入空白来向右边靠拢。另外两个相类似的函式是 <tt class="function">string.ljust()</tt> 以及 <tt class="function">string.center()</tt> 。这些函式本身并没有印出什么东西来,他们只是传回一个新的字串。如果传回的字串太长了,他们也不会截断它,他们只是单纯的传回这个新的字串。这有可能会使你的一栏一栏的格式变成乱七八糟,但是这样做通常比其他的可能要好很多(可能会造成不正确的结果)。(如果你真想把多余的部分截掉,你可以使用一个切割的动作,例如 "<tt class="samp">string.ljust(x, n)[0:n]</tt>" ) 。 </p> <p> 另外有一个函式叫做 <tt class="function">string.zfill()</tt> 这个函式会使的数目字的字串加入前头的0。该加入正负号的时候它也会自动加入: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> import string<br>>>> string.zfill('12', 5)<br>'00012'<br>>>> string.zfill('-3.14', 7)<br>'-003.14'<br>>>> string.zfill('3.14159265359', 5)<br>'3.14159265359'<br></pre> </dd> </dl> <p> 你如果使用 <code>%</code> 运算元的话结果会看起来像这样: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> import math<br>>>> print 'The value of PI is approximately %5.3f.' % math.pi<br>The value of PI is approximately 3.142.<br></pre> </dd> </dl> <p> 如果在你的格式化字串(format string)中有超过一个以上的格式存在,你要在 <code>%</code> 的右边传入一个tuple。例如这个例子: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}<br>>>> for name, phone in table.items():<br>... print '%-10s ==> %10d' % (name, phone)<br>... <br>Jack ==> 4098<br>Dcab ==> 7678<br>Sjoerd ==> 4127<br></pre> </dd> </dl> <p> 大部分的格式(format)其效果都与你在C里面所用的一样,你必须要在右边传入适当型态的资料。如果你没有正确的如此做时,你会得到一个例外的状况(exception),而不是得到一个系统核心倾倒出来的记忆体资料(dump)。其中 <code>%s</code> 这个格式最为自由,你可以使用字串或非字串,如果你使用非字串的资料时,资料会自动用内建的 <tt class="function">str()</tt> 函式转换成字串资料。你也可以使用 <code>*</code> 来传入一个独立的(整数)参数来决定宽度或是精确度(precision)的大小。但是,C里面的 <code>%n</code> 以及 <code>%p</code> 在Python里面却没有支援。 </p> <p> 如果你有一个很长的格式化字串,而你又不想分开他们的话,你可以使用名称而非位置来使用这些变数。其方法是使用C格式的延伸形式: <code>%(name)format</code> ,举例如下: </p> <p> </p> <dl> <dd><pre class="verbatim">>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}<br>>>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table<br>Jack: 4098; Sjoerd: 4127; Dcab: 8637678<br></pre> </dd> </dl> <p> 这个功能当与新的内建函式 <tt class="function">vars()</tt> 一起使用时特别有用,这个内建函式会传回一个含有所有local变数名称及值的dictionary。 </p> <p> </p> <h1> <br> 7.2 读写档案 </h1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -