Reading newlines from a text file

Robert Brewer fumanchu at amor.org
Sat Feb 28 14:55:26 EST 2004


Thomas Philips wrote:
> 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?

Hi Thomas,

In what environment or tool are you executing "print"? Pythonwin, for
example, works as expected:

>>> x = '1\n234\n567'
>>> x
'1\n234\n567'
>>> print x
1
234
567


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list