stripping blanks

M

micklee74

hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz
<---newline

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
#if not re.findall(r'^$',line):
print line

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

What can i do to make it print all..?
thanks
 
S

seeker

hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz
<---newline

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
#if not re.findall(r'^$',line):
print line

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

What can i do to make it print all..?
thanks
Hi,

this should work better:


f = open("test.dat")
while 1:
line = f.readline().rstrip("\n")
if line == '':
continue
#if not re.findall(r'^$',line):
print line
 
S

seeker

hi
i have a file test.dat eg

abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz
<---newline

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
#if not re.findall(r'^$',line):
print line

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

What can i do to make it print all..?
thanks

Last suggestion I made was bad. Here is the new one. Hopefully thats
correct.

f = open("test.dat")
for line in f:
printLine = line.rstrip("\n")
if not printLine:
continue
print printLine
 
F

Fulvio

Alle 17:06, martedì 02 maggio 2006, seeker ha scritto:
     printLine = line.rstrip("\n")

I think that nobody considered if the text has (\r) or (\r\n) or (\n) at the
end of the line(s).
 
D

Duncan Booth

Fulvio said:
Alle 17:06, martedì 02 maggio 2006, seeker ha scritto:

I think that nobody considered if the text has (\r) or (\r\n) or (\n)
at the end of the line(s).
You think wrongly.

The suggested code opens the file in text mode so the line endings are
automatically normalised to '\n'.
 
F

Fredrik Lundh

Fulvio said:
I think that nobody considered if the text has (\r) or (\r\n) or (\n) at the
end of the line(s).

if it's opened in text mode (the default), and is a text file, it will
always have "\n" at the end of the line.

</F>
 
S

Scott David Daniels

seeker said:
Last suggestion I made was bad. Here is the new one. Hopefully thats
correct.

f = open("test.dat")
for line in f:
printLine = line.rstrip("\n")
if not printLine:
continue
print printLine

A little better:

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

--Scott David Daniels
(e-mail address removed)
 
E

Edward Elliott

Scott said:
A little better:

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

[sys.stdout.write(line) for line in open('test.dat') if line.rstrip('\n')]

Where's my prize? What do you mean, shorter isn't always better? ;)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top