perl chomp equivalent in python?

Jeff Bauer jbauer at rubic.com
Thu Feb 10 17:15:27 EST 2000


Justin Sheehy wrote:
> Jeff Bauer <jbauer at rubic.com> writes:
> > Justin Sheehy wrote:
> > <snip>
> > > The need for this is also somewhat doubtful.  I have yet
> > > to see a real (not contrived) example where string.rstrip()
> > > wasn't the right choice for this sort of thing.
> >
> > In most cases trailing whitespace is probably insignificant,
> > but it would be best not to assume this for all circumstances
> > (e.g. tab-delimited files).
> 
> If you really feel the need to be more careful, you could do
> something like:
[snip]

Yes, this is similar to what I do.

> However, in comparison to the original solution of stripping
> a number of characters off without even checking what they are,
> string.rstrip() is definitely preferable.

I agree that indiscriminate use of truncating end of line
chars without checking is not ideal.

> As I said before, in every real case I've seen for this 
> sort of thing, string.rstrip() was the Right Thing to do.
> 
> > I would not want to see string.rstrip() used in library
> > modules, since it could result in a loss of data where the
> > user might not be expecting whitespace to disappear.
> 
> That's silly.  It should be used in any library module where its
> behavior (stripping all whitespace from the end of a string) is
> desired.  In any case where that is not desired, of course it
> shouldn't be used.  It's just a matter of knowing what your 
> intentions and specifications are.

I was referring to having string.rstrip() used as an idiom 
to read lines of text files.  My reluctance to see it used
in this manner in a library module is that the author of a 
library can't always know how it their library is intended 
to be used.

My non-contrived example of processing tab-delimited files 
is where use of this idiom would cause problems.

> > Of all newgroups, certainly comp.lang.python would be
> > sympathetic to this issue. ;-)
> 
> Last time I checked, Python wasn't sensitive to extra 
> whitespace at the _end_ of a line.

You mean like <backslash><space> vs. <backslash> ?
<0.2 wink>

-Jeff




More information about the Python-list mailing list