What is built-in method sub

J dreadpiratejeff at gmail.com
Mon Jan 11 15:44:49 EST 2010


On Mon, Jan 11, 2010 at 15:02, Jeremy <jlconlin at gmail.com> wrote:
> I am using the re.sub command to remove trailing whitespace from lines
> in a text file.  The commands I use are copied below.  If you have any
> suggestions on how they could be improved, I would love to know.

Just curious, but if each line is simply a string, wouldn't it be
easier to pass each one through .strip() instead?

Asking because I'm keen to know.  It's been a while since I bothered
with any real regluar expressions, and while I've touched on them in
bash programming and some perl, I've never touched them in Python.

But just from that little big of code, it seems to me that if each
item in lines is just a string object that is one line from the file,
then just passing it to .strip() to remove the whitespace is quick and
simple.  Unless there's a reason to using the overhead (assuming
python has the same or similar overhead to regular expressions that
sometimes occur in other languages).

Cheers,

Jeff



-- 

Pablo Picasso  - "Computers are useless. They can only give you
answers." - http://www.brainyquote.com/quotes/authors/p/pablo_picasso.html



More information about the Python-list mailing list