Reading newlines from a text file

John Roth newsgroups at jhrothjr.com
Sat Feb 28 16:28:55 EST 2004


"Thomas Philips" <tkpmep at hotmail.com> wrote in message
news:b4a8ffb6.0402281000.606425e2 at posting.google.com...
> I have a data file that I read with readline(), and would like to
> control the formats of the lines when they are printed. I have tried
> inserting escape sequences into the data file, but am having trouble
> getting them to work as I think they should. For example, if my data
> file has only one line which reads:
> 1\n234\n567
>
> I would like to read it with a command of the form
> x=datafile.readline()
>
> and I would like
> print x
>
> to give me
> 1
> 234
> 567
>
> The readline works like a charm, but the print gives me
> 1\n234\n567
> Clearly the linefeeds are not interpreted as such. How can I get the
> Python interpreter to correctly interpret escape sequences in strings
> that are read from files?
>
> Thomas Philips

You've got a couple of misconceptions. If you use a standard
open(<file>, "rt"), Python will only recognize end of line sequences
for your system. Windows, unices and the Mac all have different
conventions, so if you're on Windows and your file has unix or mac
newline sequences, they won't be recognized. They'll come in on
one readline().

The second misconception seems to be that Python will strip
newlines when it reads in line mode. It doesn't. It does convert
the OS dependent newline sequences to a standard \n, but that's
all. Each line read is still has a newline at the end (except the last one,
if it was missing in the file.)

Print, on the other hand, is going to add a newline regardless of
whether one exists in the data.

You can solve the first problem by adding a "U" somewhere in
the open/file call. I'm not sure where, check the docs. To solve
the second problem, you need to do one of several things:

1) if you want to use print, strip the newline from the string
before writing it. The print statement will add it to the end.

2) if all you want is line endings for your system, open with
"wt" and put a /n at the end of each line. Python will take care
of the rest.

3) if you want line endings for a different system, open the file
as 'wb' and insert the proper sequence at the end yourself.

HTH
John Roth





More information about the Python-list mailing list