how to avoid leading white spaces
D'Arcy J.M. Cain
darcy at druid.net
Fri Jun 3 10:58:57 EDT 2011
On 03 Jun 2011 14:25:53 GMT
Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> source.replace(",", " ").split(" ")
I would do;
source.replace(",", " ").split()
> [steve at sylar ~]$ python -m timeit -s "source = 'a b c,d,e,f,g h i j k'"
What if the string is 'a b c, d, e,f,g h i j k'?
>>> source.replace(",", " ").split(" ")
['a', 'b', 'c', '', 'd', '', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
>>> source.replace(",", " ").split()
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
Of course, it may be that the former is what you want but I think that
the latter would be more common.
> There's no need to use a regex just because you think that you *might*,
> someday, possibly need a regex. That's just silly. If and when
> requirements change, then use a regex. Until then, write the simplest
> code that will solve the problem you have to solve now, not the problem
> you think you might have to solve later.
I'm not sure if this should be rule #1 for programmers but it
definitely needs to be one of the very low numbers. Trying to guess
the client's future requests is always a losing game.
--
D'Arcy J.M. Cain <darcy at druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
More information about the Python-list
mailing list