cut off \n

Dave Kuhlman dkuhlman at rexx.com
Mon Jun 9 13:00:44 EDT 2003


Tom wrote:

> Hi,
> 
> 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.
> 

Are you using Python 2.2.3 or later?

If so, take a look at the rstrip function in the string module.
See:

    http://www.python.org/doc/current/lib/module-string.html

Then try 

    string.rstrip(line, '\n')

  - Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list