Newline at EOF Removal

A

Alex Nordhus

I am looking for a way to strip the blank line and the empty newline at
the end of the text file. I can get the blank lines removed from the
file but it always leaves the end line (which is blank) as a newline. My
code is here and it works but leaves the newline at the end of the file.
How do I get rid of it?

import re
f = open("oldfile.txt")
w = open("newfile.txt", "wt")
for line in f.readlines():
line = re.sub(r'^\s+$', '', line)


w.write(line)

w.close()

I have tried everything I know and still falling short. Any help?
 
M

Mike Meyer

Alex Nordhus said:
I am looking for a way to strip the blank line and the empty newline at
the end of the text file. I can get the blank lines removed from the
file but it always leaves the end line (which is blank) as a newline. My
code is here and it works but leaves the newline at the end of the file.

This really isn't a very clear description of the problem. Does your
file end with two newlines in row? That would be what you'd get if it
ended with the newline for the last line. If it ends with just one
newline, then you're not getting the a blank last line, you're simply
getting a last line that ends with a newline.
How do I get rid of it?

import re
f = open("oldfile.txt")
w = open("newfile.txt", "wt")
for line in f.readlines():
line = re.sub(r'^\s+$', '', line)
w.write(line)

This is an abuse of regular expressions (see
http://www.mired.org/home/mwm/spare/ ); simple string methods can do
this job for you. Try:

for line in f:
if line.strip():
w.write(line)



<mike
 
A

Alex N

Actually Im doing a process with PHP, Unicode, Ansi. I am trying to
format these text files to be acceptable to PHP. The first process is
to strip the unicode and convert to ansi. But there are blank lines in
the file and at the end of the file. I was just having trouble with the
lines at the end. PHP blows up if there is a blank or empty line in the
text files. So thats where i was at.
Peter gave me a good clue here
w.write(f.read().rstrip('\n') + '\n')
However the 2nd \n puts that empty line at the end of the file so I
tried it with just
w.write(f.read().rstrip('\n'))
and it works great!

Now I can go on to the next step. Thank you guys for your help

Alex
 
B

Bengt Richter

I am looking for a way to strip the blank line and the empty newline at
the end of the text file. I can get the blank lines removed from the
file but it always leaves the end line (which is blank) as a newline. My
code is here and it works but leaves the newline at the end of the file.
How do I get rid of it?

import re
f = open("oldfile.txt")
w = open("newfile.txt", "wt")
for line in f.readlines():
line = re.sub(r'^\s+$', '', line)


w.write(line)

w.close()

I have tried everything I know and still falling short. Any help?

First, you don't need re for that, and second, why write a zero-length ''?
It might even confuse some file system. You don't need readlines either. Try
(untested)
# open f and w as before [1]
for line in f:
if line.strip(): w.write(line) # write only non-blank lines

[1] BTW, I didn't see the 't' mode in http://docs.python.org/lib/built-in-funcs.html
description of open/file, but I have a nagging doubt about saying it's not valid.
Where did you see it?

Regards,
Bengt Richter
 
A

Alex N

Thank you guys for all your help. Was able to nail it down.
uh I dunno where i saw the 'wt' at. somewhere online though.
I have to do it in 2 stages though and run through a coula files but it
works.
import re
f = open("oldfile.txt")
w = open("newfile.txt", "w")
for line in f:
if line.strip():

w.write(line)


f.close()
w.close()

g = open("newfile.txt")
x = open("mynewfile.txt", "w")

x.write(g.read().rstrip('\n'))

x.close()

Got around the I/O errors luckily
thanks guys
 
T

Tim Peters

[Bengt Richter]
...
[1] BTW, I didn't see the 't' mode in http://docs.python.org/lib/built-in-funcs.html
description of open/file, but I have a nagging doubt about saying it's not valid.
Where did you see it?

't' is a Windows-specific extension to standard C's file modes.
Python passes mode strings as-is on to the platform C library, so if
your platform C likes 't', you're free to use it.

You might think there's no point to passing 't', since text mode is
the default. If so, you'd almost be right ;-). The rub is that
Windows also supports another non-standard gimmick, to make binary
mode the global default instead (although very few know about this,
and I've never seen it used in real life). If you've done that, then
't' is necessary to get text mode.
 
M

Mike Meyer

Alex N said:
Peter gave me a good clue here
w.write(f.read().rstrip('\n') + '\n')
However the 2nd \n puts that empty line at the end of the file so I

No, it doesn't. Well, maybe it doesn't, depending on how your
applications treats newlines. Normally, a newline terminates a line,
so a file ending in "...some-alpha-text\n" ends in a complete
line. Python agrees with this, as file.readlines won't return a list
with a final empty string in it if fed a file whose last character is
a newline.
tried it with just
w.write(f.read().rstrip('\n'))
and it works great!

This one ends in "...some-alpha-text". I'd say it ended in an
"incomplete" line, because the last line doesn't have a trailing
newline. My editor would agree - it warns me if I try and save a file
with an incomplete final line.

From what you've said, PHP apparently treats newlines as line
*separators*, not terminators. So a file that ends in a final newline
ends with a final blank line.

I suspect that part of your problem is trying to deal with the two
different views of what a newline does.

<mike
 

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
474,276
Messages
2,571,384
Members
48,072
Latest member
FaustoBust

Latest Threads

Top