📄 34.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 Developer's Handbook -> Input and Output</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.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="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="22.html" class="navtitle">2. Language Review</a> > <span class="nonavtitle">Input and Output</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="33.html" title="Modules and Packages"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=34" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="34.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="35.html" title="File Handling"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A20%3A17+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162146122163094222089189188</font><a href="read9.asp?bookname=0672319942&snode=34&now=5%2F31%2F2002+4%3A20%3A17+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Input and Output</h3>
<p>Python, as any other language, provides means to get input from the user and also to display information to him.</p>
<P>Let's see how we can handle it.</P>
<PRe>
>>> x = input ("type anything: ")
>>> print "You have typed ", x
</pre>
<P>Note that the input prompt can be anything, even an empty one.</P>
<P>If the user types <Tt claSS="monofont">5, x</TT> is properly treated as a number. To make <tt class="monofont">x</tt> become a string, the user must explicitly type the quotes.</p>
<p>To avoid this problem, you can use the <tt class="monofont">raw_input</tt> function:</P>
<prE>
>>> x = raw_input ("type anything: ")
>>> print "You have typed ", x
</pre>
<P>Now, it doesn't matter whether the user types the quotes.</p>
<p>Note that the <tt ClasS="monofont">print</TT> command requires objects to be separated by commas:</P>
<pre>
>>> print "parrot", "sketch"
parrot sketch
</pRE>
<H4>
Displaying Information</H4>
<p>Let's delve a little bit deeper into this topic.</p>
<p>Python has three standard file objects, which are available from the <a NAME="idx1073742568"></a>
<a naME="idx1073742569"></A>
<Tt class="monofont">sys</tt> module. The interpreter uses them to provide input and output facilities. (Refer to <a href="38.html">Chapter 3, "Python Libraries,"
</a> for details and examples梩he <tt clasS="monofont">sys</tt> module.)</P>
<p>They are known as <tt Class="monofont">sys.stdin, sys.stdout, sys.stderr</Tt>
</p>
<p>
<A NAMe="idx1073742570"></a>
<a nAME="idx1073742571"></A>
<tt clASS="monofont">print</Tt> statements are mapped to the <tt cLASS="monofont">sys.stdout.</tt> Hence, they send the textual representation of objects to the standard output stream:</p>
<pre>
>>>import sys
>>>sys.stdout.write("Nudge-nudge\n")
Nudge-nudge
</pre>
<p>Did you know that it is possible to re-map the standard output device?</p>
<p>Yes, that is possible.</p>
<p>You can run the following code to write to a file:</p>
<pre>
>>> sys.stdout = open("outputtest.txt", "w")
>>> print "hello"
>>> sys.stdout.close
>>> sys.stdout = sys.__stdout__
>>> sys.exit()
</prE>
<p>Note that <tT claSs="monofont">sys.__stdout__</tt> stores the original <tT claSS="monofont">stdout.</TT>
</p>
<p>The last line restores the <tt CLASs="monofont">sys.__stdout__</tt> original value to such an extent that new <tT CLAss="monofont">print</tt> statements will display onscreen, instead of being sent to a file.</P>
<P>As additional information, this program uses <TT class="monofont">sys.exit()</tt> to quit its execution (refer to <a href="38.html">Chapter 3</a> for details).<a name="idx1073742572"></a>
<a NamE="idx1073742573"></a>
<a nAme="idx1073742574"></a>
<a Name="idx1073742575"></A>
<A NAme="idx1073742576"></a>
</p>
<P>Starting with release 2.0, the <TT Class="monofont">print</TT> statement <A Name="idx1073742577"></a>
<A NAMe="idx1073742578"></a>
<a name="idx1073742579"></a>can have its output directed to a file-like object, as it is demonstrated in the following example.</p>
<pre>
print >> sys.stderr, "Sorry, you cannot do that!"
</pre>
<h4>
Formatting Operations</h4>
<p>Python provides formatting operations similar to the <tt ClaSs="monofont">printf()</tt> function from the C language.</P>
<p>Take a look at the following example:</p>
<prE>
>>> print "Mr. Lumberjack! do not sing!"
</pre>
<P>What if you don't want to hard-code the name inside the string? Compare the previous line of code against the following one:</P>
<PRe>
>>> print "Mr. %s, do not sing!" % someone
</pre>
<P>Flexible, don't you think? And by the way, the order of the elements doesn't affect the final result.</P>
<P>Therefore, saying</P>
<pre>
>>> print "Mr. %s" % someone
</pRE>
<P>is the same as saying</P>
<pre>
>>> print someone % "Mr. %s"
</pRE>
<P>As a matter of fact, the following example shows how Python handles multiple format arguments. Note that you need to provide a tuple of values to fill the position indicated by the formatting operators (see <A href="34#3.html">Table 2.2</a>).<a name="idx1073742580"></a>
<a name="idx1073742581"></a>
<a naMe="idx1073742582"></a>
<A namE="idx1073742583"></a>
<a naMe="idx1073742584"></a>
</p>
<PRE>
>>> print "The %s has %i wings" % ("parrot", 2)
</Pre>
<a nAME="3"></A><p><tabLE BOrder="1" CELLspacing="0" cellpadding="1" wiDth="100%">
<CaptIon><h5>Table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -