[Tutor] Reading & printing lines from two different files

stuart_clemons@us.ibm.com stuart_clemons@us.ibm.com
Thu, 6 Jun 2002 15:02:36 -0400


--0__=0ABBE143DFF43EE28f9e8a93df938690918c0ABBE143DFF43EE2
Content-type: text/plain; charset=US-ASCII


Thanks Jeff.  Your structure works perfectly.  I really appreciate the
intro to this structure and also the explanation of sys.stdout.write()!  I
think I can really build off of this.

It's coming slowly, but I'm learning.

Thanks again,

- Stuart

---------------------- Forwarded by Stuart Clemons/Westford/IBM on
06/06/2002 03:09 PM ---------------------------


"Jeff Shannon" <jeff@ccvcorp.com> on 06/06/2002 02:48:15 PM

To:    stuart_clemons@us.ibm.com
cc:    tutor@python.org
Subject:    Re: [Tutor] Reading & printing lines from two different files



stuart_clemons@us.ibm.com wrote:

> Hi all:
>
> I have two files, file1 and file2. I want to print the first line
> from file1, then the first line from file2, then the second line
> from file 1, then the second line from file 2, etc.

What do you want to do if one file is shorter than the other?

The basic method I would use would probably go something like this --

import sys

file1 = open('file1', 'r')
file2 = open('file2', 'r')

while 1:
    line1 = file1.readline()
    sys.stdout.write(line1)
    line2 = file2.readline()
    sys.stdout.write(line2)
    if not line1 and not line2:
        break

This will print interleaved lines until one file is exhausted, print
the remaining lines from the other file, and then break.  You can
alter this behavior by adjusting the conditions for the break
statement.  For example, 'if not line1 or not line2' would cause
printing to stop as soon as either file is exhausted.

You're probably wondering what that sys.stdout.write() stuff is.  I
used that instead of print, because print will add newlines (and
sometimes spaces).  Often this is useful, but since you're reading
lines from a file, they already have a newline at the end of them.  I
*could* strip that newline off before printing, but there's another
problem.  When one file is exhausted (we've reached EOF on it),
readline() will return an empty string, and printing that empty string
will output an extra space.  I could test for an empty line, too
(either before or after stripping the newline), but it's easier to use
sys.stdout.write(), which sends an exact string to the standard
output, without doing any of the "helpful" formatting that print
does.  This way, I use the newlines from the file without needing to
fiddle with them, and write()ing an empty string does nothing --
exactly what I want.

Hope this helps...

Jeff Shannon
Technician/Programmer
Credit International




--0__=0ABBE143DFF43EE28f9e8a93df938690918c0ABBE143DFF43EE2
Content-type: text/html; charset=US-ASCII
Content-Disposition: inline

<html><body>
<p>Thanks Jeff.  Your structure works perfectly.  I really appreciate the intro to this structure and also the explanation of sys.stdout.write()!  I think I can really build off of this.<br>
<br>
It's coming slowly, but I'm learning.<br>
<br>
Thanks again,<br>
<br>
- Stuart<br>
<br>
<font color="#800080">----------------------</font><font size="2" color="#800080"> Forwarded by Stuart Clemons/Westford/IBM on 06/06/2002 03:09 PM </font><font color="#800080">---------------------------</font><br>

<p><font size="2" color="#800080">To:	</font><font size="2">stuart_clemons@us.ibm.com</font><br>
<font size="2" color="#800080">cc:	</font><font size="2">tutor@python.org</font><font size="2" color="#800080"> </font><br>
<font size="2" color="#800080">Subject:	</font><font size="2">Re: [Tutor] Reading &amp; printing lines from two different files</font><br>
<br>
<font face="Courier New"><br>
<br>
stuart_clemons@us.ibm.com wrote:<br>
<br>
&gt; Hi all:<br>
&gt;<br>
&gt; I have two files, file1 and file2. I want to print the first line<br>
&gt; from file1, then the first line from file2, then the second line<br>
&gt; from file 1, then the second line from file 2, etc.<br>
<br>
What do you want to do if one file is shorter than the other?<br>
<br>
The basic method I would use would probably go something like this --<br>
<br>
import sys<br>
<br>
file1 = open('file1', 'r')<br>
file2 = open('file2', 'r')<br>
<br>
while 1:<br>
    line1 = file1.readline()<br>
    sys.stdout.write(line1)<br>
    line2 = file2.readline()<br>
    sys.stdout.write(line2)<br>
    if not line1 and not line2:<br>
        break<br>
<br>
This will print interleaved lines until one file is exhausted, print<br>
the remaining lines from the other file, and then break.  You can<br>
alter this behavior by adjusting the conditions for the break<br>
statement.  For example, 'if not line1 or not line2' would cause<br>
printing to stop as soon as either file is exhausted.<br>
<br>
You're probably wondering what that sys.stdout.write() stuff is.  I<br>
used that instead of print, because print will add newlines (and<br>
sometimes spaces).  Often this is useful, but since you're reading<br>
lines from a file, they already have a newline at the end of them.  I<br>
*could* strip that newline off before printing, but there's another<br>
problem.  When one file is exhausted (we've reached EOF on it),<br>
readline() will return an empty string, and printing that empty string<br>
will output an extra space.  I could test for an empty line, too<br>
(either before or after stripping the newline), but it's easier to use<br>
sys.stdout.write(), which sends an exact string to the standard<br>
output, without doing any of the &quot;helpful&quot; formatting that print<br>
does.  This way, I use the newlines from the file without needing to<br>
fiddle with them, and write()ing an empty string does nothing --<br>
exactly what I want.<br>
<br>
Hope this helps...<br>
<br>
Jeff Shannon<br>
Technician/Programmer<br>
Credit International<br>
<br>
<br>
</font><br>
<br>
</body></html>
--0__=0ABBE143DFF43EE28f9e8a93df938690918c0ABBE143DFF43EE2--