Equivalent of Perl chomp?

Daniel Dittmar daniel.dittmar at sap.com
Thu Jan 31 10:42:00 EST 2002


> def chomp(s):
>     return s[:-1]
>
> As long as you read text without binary mode, this is all you really need.

Unfortunately, there is the occasional file where the last line doesn't end
in a '\n'.

Personally, I'd prefer not a new string method, but  a 'strip-nl' option for
.readline () and .readlines (). But this will probably break lots of classes
implementing 'file-like' behaviour.

Daniel






More information about the Python-list mailing list