mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
finished #for section
This commit is contained in:
+3
-5
@@ -224,13 +224,11 @@ ValueError: I/O operation on closed file.</samp>
|
||||
line_number += 1
|
||||
<a> print('{} {}'.format(line_number, a_line.rstrip())) <span class=u>③</span></a></code></pre>
|
||||
<ol>
|
||||
<li>FIXME
|
||||
<li>
|
||||
<li>
|
||||
<li>Using <a href=#with>the <code>with</code> pattern</a>, you safely open the file and let Python close it for you.
|
||||
<li>This is it: to read a file one line at a time, use a <code>for</code> loop. That’s it. Besides having explicit methods like <code>read()</code>, <em>the file object is also an <a href=iterators.html>iterator</a></em> which spits out a single line every time you ask for a value.
|
||||
<li>Using <a href=strings.html#formatting-strings>the <code>format()</code> string method, you can print out the line number and the line itself. (The <var>a_line</var> variable contains the complete line, carriage returns and all. The <code>rstrip()</code> string method removes the trailing whitespace, including the carriage return characters.)
|
||||
</ol>
|
||||
|
||||
<p>FIXME
|
||||
|
||||
<pre class=screen>
|
||||
<samp class=p>you@localhost:~/diveintopython3$ </samp><kbd class=pp>python3 examples/oneline.py</kbd>
|
||||
<samp>1 Dora
|
||||
|
||||
Reference in New Issue
Block a user