cut off \n

Just just at xs4all.nl
Mon Jun 9 20:33:25 CEST 2003


In article <3EE4ACCD.70000 at gmx.net>, Tom <llafba at gmx.net> wrote:

> I have to read data from a file to a list. Unfortunately most of the 
> data that I read into my little list ends with a linefeed \n.
> Right now I get rid of this by just cutting off the last character. (a = 
> l[:-1]).
> My problem is that sometimes the last line of the file has a \n and 
> sometimes not. With the method above I sometimes cut off parts of my 
> string. So it would be much nicer if I can find out if the data does 
> have a \n and then cut it off.
> 
> This is probably a very common problem but I am pretty new to Phython 
> and could find anything useful with google. :-(

I your files aren't very big, then you could do something like this:

    f = file(path)
    data = f.read()
    f.close()
    lines = data.splitlines()

Just




More information about the Python-list mailing list