stripping

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
 
E

Edward Elliott

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

you want continue, not break there. but that gives you an infinite loop, so
you need a new exit condition. you're better off scrapping the while and
using a for loop or list comprehension on readline.
 
J

Jesse Hager

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
Change the 'break' statement to a 'continue'.
 

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
474,294
Messages
2,571,511
Members
48,202
Latest member
ClaudioVil

Latest Threads

Top