What is built-in method sub

Terry Reedy tjreedy at udel.edu
Mon Jan 11 15:47:08 EST 2010


On 1/11/2010 3:02 PM, Jeremy wrote:

> I am using the re.sub command to remove trailing whitespace from lines
> in a text file.

 >>> help(str.rstrip)
Help on method_descriptor:

rstrip(...)
     S.rstrip([chars]) -> str

     Return a copy of the string S with trailing whitespace removed.
     If chars is given and not None, remove characters in chars instead.

Should be mush faster.

  The commands I use are copied below.  If you have any
> suggestions on how they could be improved, I would love to know.
>
> Thanks,
> Jeremy
>
> lines = self._outfile.readlines()
> self._outfile.close()
>
> line = string.join(lines)
>
> if self.removeWS:
>      # Remove trailing white space on each line
>      trailingPattern = '(\S*)\ +?\n'
>      line = re.sub(trailingPattern, '\\1\n', line)





More information about the Python-list mailing list