Some Python 2.1 ideas

Bjorn Pettersen pbjorn at uswest.net
Mon Dec 25 14:23:15 EST 2000


Greg Jorgensen wrote:

> [snip]

> I always use rstrip because I never care about trailing whitespace. If I did
> have a reason to preserve trailing spaces I would write my own code to strip
> the trailing newlines.

Good enough, but it's error prone to have everyone write their own version of
chomp:

    def chomp(s):
        if s.endswith('\r\n'): # dos
            return s[:-2]
        if s.endswith('\n'): # unix
            return s[:-1]
        if s.endswith('\r'): # mac
            return s[:-1]
        return s # no terminator

(I actually forgot the case where the string doesn't have a line terminator the
first time I wrote it...)

-- bjorn




More information about the Python-list mailing list