strip newlines and blanks

M

micklee74

hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz


I wish to print the contents of the file such that it appears:
abcdefgh
ijklmn
opqrs
tuvwxyz

here is what i did:
f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
break
print line

but it always give me first 2 lines, ie
abcdefgh
ijklmn

What can i do to make it print all w/o the newlines..? and what is the
proper way to skip printing blank lines while iterating file contents?

thanks
 
P

Peter Otten

(e-mail address removed) wrote:

Mick, you should be a bit more patient. Allow for some time for an answer to
arrive. Minor edits of your question don't warrant a repost.
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz


I wish to print the contents of the file such that it appears:
abcdefgh
ijklmn
opqrs
tuvwxyz

here is what i did:
f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
break
print line

but it always give me first 2 lines, ie
abcdefgh
ijklmn

What can i do to make it print all w/o the newlines..? and what is the
proper way to skip printing blank lines while iterating file contents?

The end of the file is signalled by an empty string, and a blank line with
the trailing "\n" stripped off is an empty string, too. Therefore you have
to perform the line == '' test before the stripping.

Here's an alternative approach:

for line in f:
line = line.rstrip()
if line:
print line

This will ignore any lines containing only whitespace and remove trailing
whitespace from the non-white lines.

Peter
 
F

Fredrik Lundh

i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz


I wish to print the contents of the file such that it appears:
abcdefgh
ijklmn
opqrs
tuvwxyz

here is what i did:
f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
break
print line

but it always give me first 2 lines, ie
abcdefgh
ijklmn

What can i do to make it print all w/o the newlines..? and what is the
proper way to skip printing blank lines while iterating file contents?

assuming you want to skip *all* blank lines (even if they have whitespace
on them),

for line in open("test.dat"):
if line.strip():
print line

</F>
 
M

micklee74

thanks..and sorry, i am using the web version of google groups and
didn't find an option i can edit my post, so i just removed it..
thanks again for the reply..
 
S

Sybren Stuvel

(e-mail address removed) enlightened us with:
thanks..and sorry, i am using the web version of google groups and
didn't find an option i can edit my post

It's usenet, you can't edit posts.
so i just removed it..

Which doesn't work at all. Stupid thing they allow you to try and
delete something on Google Groups, since removal of Usenet posts is
ignored by most Usetnet carriers.

Sybren
 
K

Kent Johnson

hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz


I wish to print the contents of the file such that it appears:
abcdefgh
ijklmn
opqrs
tuvwxyz

here is what i did:
f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
break

break terminates the loop, so no more lines will be processed. Use
continue, which ends only the current iteration of the loop. (Though you
will need a separate test to terminate the loop when there are no more
lines.)

You can iterate an open file directly; here is a shorter version:

for line in open('test.dat'):
line = line.rstrip('\n')
if line:
print line

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top