Some Python 2.1 ideas

Bjorn Pettersen pbjorn at
Sun Dec 24 14:24:13 EST 2000

Greg Jorgensen wrote:

> "Bjorn Pettersen" <pbjorn at> wrote in message
> [snip]
> How should these be handled?
>   "foo\n\n\n"
>   "foo\n\r\n\r"

"foo\n\n\n" -> "foo\n\n"
"foo\n\r\n\r" -> "foo\n\r"

> In other words, does the chomp() method always remove 0-2 characters, or all
> trailing /n/r characters? Is it a good idea to add a string method that

string.rstrip() is probably sufficient if you want to strip all whitespace from
the end...

> makes assumptions about what the file.readline and file.readlines methods
> return? Wouldn't it be cleaner to enhance the file object with additional
> methods:
>   f. readline_chomp()
>   f.readlines_chomp()

This would be a good idea, except it wouldn't fit with the standard Python idiom
for reading files:

   while 1:
      line = f.readline_chomp()
      if not line: break # would always break

> Or perhaps add an attribute to the file object to indicate if newlines
> should be stripped for text files?
>   f.chomp = 1  # 1=strip trailing /n and /r characters, 0=leave them
> (default)

I'd be hesitant about adding complexity to the file object interface since any
changes might have to be cascaded into StringIO etc.

More information about the Python-list mailing list