cut off \n

Skip Montanaro skip at pobox.com
Mon Jun 9 12:12:02 EDT 2003


    Tom> Right now I get rid of this by just cutting off the last
    Tom> character. (a = l[:-1]).  My problem is that sometimes the last
    Tom> line of the file has a \n and sometimes not. 

Try

    a = l.rstrip() 

instead.  If there is significant whitespace at the end of lines, then

    a = l
    if l[-1:] == '\n':
        a = l[:-1]

will work.

Skip





More information about the Python-list mailing list