Reading newlines from a text file

Thomas Philips tkpmep at hotmail.com
Sat Feb 28 13:00:38 EST 2004


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



More information about the Python-list mailing list