Idiomatic portable way to strip line endings?

Sean 'Shaleh' Perry shalehperry at attbi.com
Sun Dec 16 13:21:03 EST 2001


> 
> def removecrlf( line):
>     """Remove \r\n or \n from end of string."""
>     if line[-1:] == '\n':
>         if line[-2:-1] == '\r':
>             return line[:-2]
>         else:
>             return line[:-1]
>     else:
>         return line
>     

so on the worst case you create 3 strings to return one.  Ick.




More information about the Python-list mailing list