<br><div><span class="gmail_quote">On 1 May 2006 23:20:56 -0700, <b class="gmail_sendername"><a href="mailto:micklee74@hotmail.com">micklee74@hotmail.com</a></b> <<a href="mailto:micklee74@hotmail.com">micklee74@hotmail.com
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">hi<br>i have a file test.dat eg<br><br>abcdefgh<br>ijklmn<br>         <-----newline
<br>opqrs<br>tuvwxyz<br>          <---newline<br><br><br>I wish to print the contents of the file such that it appears:<br>abcdefgh<br>ijklmn<br>opqrs<br>tuvwxyz<br><br>here is what i did:<br>f = open("test.dat")
<br>while 1:<br>        line = f.readline().rstrip("\n")<br>        if line == '':<br>                break<br>        #if not re.findall(r'^$',line):<br>        print line<br><br>but it always give me first 2 lines, ie
<br>abcdefgh<br>ijklmn<br><br>What can i do to make it print all..?</blockquote><div><br>
The break is terminating the while loop after the first blank line,
replacing it with a continue would solve the problem in your script. <br>
<br>
However:<br>
<br>
f = open("test.dat")<br>
while 1:<br>
        line = f.readline().rstrip("\n")<br>
        if line:<br>
             print line<br>
<br>
is simpler and easier to read.   And:<br>
<br>
>>> f = open("test.dat").read()  # read the whole file at once into string f<br>
>>> print f.replace('\n\n','\n')<br>
abcdefgh<br>
ijklmn<br>
opqrs<br>
tuvwxyz<br>
<br>
>>><br>
<br>
even simpler,  as long as the file isn't huge.<br>
<br>
If you need the final newline removed, use:<br>
<br>
>>>print f.replace('\n\n','\n')[:-1]<br>
abcdefgh<br>

ijklmn<br>

opqrs<br>

tuvwxyz<br>

>>><br>
<br>
HTH :)<br>
<br>
<br>
<br>
<br>
<br>
 <br>
</div><br></div>