What is the best way to handle a missing newline in the following case

C

chad

I have an text file with the following numbers

1
3
5
7
3
9



Now the program reads in this file. When it encounters a '\n', it will
do some stuff and then move to the next line. For example, it will
read 1 and then '\n'. When it sees '\n', it will do some stuff and go
on to read 3.

The problem is when I get to the last line. When the program sees '\n'
after the 9, everything works fine. However, when there isn't a '\n',
the program doesn't process the last line.

What would be the best approach to handle the case of the possible
missing '\n' at the end of the file?
 
P

Peter Otten

chad said:
I have an text file with the following numbers

1
3
5
7
3
9



Now the program reads in this file. When it encounters a '\n', it will
do some stuff and then move to the next line. For example, it will
read 1 and then '\n'. When it sees '\n', it will do some stuff and go
on to read 3.

The problem is when I get to the last line. When the program sees '\n'
after the 9, everything works fine. However, when there isn't a '\n',
the program doesn't process the last line.

What would be the best approach to handle the case of the possible
missing '\n' at the end of the file?

Don't split the data into lines yourself, delegate to python
.... f.write("1\n2\n3\r\n4\r5")
........ print repr(line)
....
'1\n'
'2\n'
'3\n'
'4\n'
'5'

As you can see "\n", "\r\n" and "\r" are all converted to "\n". This is
called universal newline mode and enabled by open(..., "U"). If your client
code insists that a line has to end with "\n" and there's no way to change
that you can wrap the file
.... prev = next(instream)
.... for cur in instream:
.... yield prev
.... prev = cur
.... if not prev.endswith("\n"):
.... prev += "\n"
.... yield prev
........ print repr(line)
....
'1\n'
'2\n'
'3\n'
'4\n'
'5\n'

But often the opposite direction, removing any newlines, works just as well
and is easier to achieve:
.... print repr(line.strip("\n"))
....
'1'
'2'
'3'
'4'
'5'

Peter
 
D

danmcleran

The problem is when I get to the last line. When the program sees '\n'
after the 9, everything works fine. However, when there isn't a '\n',
the program doesn't process the last line.

What would be the best approach to handle the case of the possible
missing '\n' at the end of the file?

use readines to read all lines into a list and then iterate thru the
list:

f = open(r'c:\test.txt', 'rb')
print f.readlines()
['1\r\n', '3\r\n', '5\r\n', '7\r\n', '3\r\n', '9']
 
P

Paul Rudin

The problem is when I get to the last line. When the program sees '\n'
after the 9, everything works fine. However, when there isn't a '\n',
the program doesn't process the last line.

What would be the best approach to handle the case of the possible
missing '\n' at the end of the file?

use readines to read all lines into a list and then iterate thru the
list:

f = open(r'c:\test.txt', 'rb')
print f.readlines()
['1\r\n', '3\r\n', '5\r\n', '7\r\n', '3\r\n', '9']

There's no real point in contructing a list. Just do

with open(r'c:\test.txt') as f:
for l in f:
print int(l)

As long as you just have digits and whitespace then that's fine - int()
will do as you want.
 
N

Neil Cerutti

The problem is when I get to the last line. When the program
sees '\n' after the 9, everything works fine. However, when
there isn't a '\n', the program doesn't process the last
line.

What would be the best approach to handle the case of the possible
missing '\n' at the end of the file?

use readines to read all lines into a list and then iterate thru the
list:

f = open(r'c:\test.txt', 'rb')
print f.readlines()
['1\r\n', '3\r\n', '5\r\n', '7\r\n', '3\r\n', '9']

There's no real point in contructing a list. Just do

with open(r'c:\test.txt') as f:
for l in f:
print int(l)

As long as you just have digits and whitespace then that's fine
- int() will do as you want.

Keep in mind that a file that a text file that doesn't end with
a newline isn't strictly legal. You can expect problems, or at
least warnings, with other tools with such files.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top