Reading newlines from a text file

Peter Otten __peter__ at web.de
Sun Feb 29 03:40:42 EST 2004


Thomas Philips wrote:

> Very neat. That is exactly what I want it to do. On looking through

After reading John Roth's post I'm no longer sure. If you want multiple
lines from a file you don't need escape sequences, just use

s = file("tmp.txt", "U").read() 

to put it all in a single string. Only if you want to interpret each line in
a file as multiple line strings, the codec hack should be considered. To
get the best solution, you'd rather post the concrete problem you are
trying to solve.

> the help for open() (or its replacement, file()), I did not see a
> reference to a "string_escape" option. Where is this documented (in
> language comprehensible to a newbie)?

The documentation is here (and on the neighbouring pages):

http://www.python.org/doc/current/lib/node127.html

Codecs are probably not the first thing you'll want to learn about Python,
though.
 
> Also, I notice that you have two backslashes before each n in
> "\\ntoerichte\\nlogik\\nboeser\\nkobold\n". Why is there a need for 2
> backslashes -would not one have done the trick?

I think Diez has already explained that. Another way to find out is to run
my example and then look into tmp.txt. There are actually two lines. Can
you find out why? Hint: the last \n is different.

Peter





More information about the Python-list mailing list