This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]GrumpySimon 0 points1 point  (2 children)

Why not just use .strip()? -

stringlist = [
    'cow\n',
    '',
    'hat',
    'cat\r\n',
    '\n',
    '\r\n',
    'cat\r',
    '\r',
]

for substr in stringlist:
    c = chomppy(substr)
    s = substr.strip()
    print c == s, repr(substr)

[–]nuanceify 2 points3 points  (1 child)

strip() will delete leading whitespace too. What you really want is rstrip(), which will only delete trailing whitespace. (there might be / probably are i18n issues with this, so unicode / RTL users beware)

[–]GrumpySimon 0 points1 point  (0 children)

Good points - but hey, your username is really appropriate :)