how to avoid leading white spaces

rurpy at yahoo.com rurpy at yahoo.com
Mon Jun 6 01:44:01 EDT 2011


On 06/03/2011 02:49 PM, Neil Cerutti wrote:
> > On 2011-06-03, rurpy at yahoo.com <rurpy at yahoo.com> wrote:
>>>> >>>> or that I have to treat commas as well as spaces as
>>>> >>>> delimiters.
>>> >>>
>>> >>> source.replace(",", " ").split(" ")
>> >>
>> >> Uhgg. create a whole new string just so you can split it on one
>> >> rather than two characters?  Sorry, but I find
>> >>
>> >>     re.split ('[ ,]', source)
> >
> > It's quibbling to complain about creating one more string in an
> > operation that already creates N strings.

It's not the time it take to create the string, its the doing
of things that aren't really needed to accomplish the task:
The re.split says directly and with no extraneous actions,
"split 'source' on either spaces or commas".  This of course
is a trivial example but used thoughtfully, REs allow you to
be very precise about what you are doing, versus using "tricks"
like substituting individual characters first so you can split
on a single character afterwards.

> > Here's another alternative:
> >
> > list(itertools.chain.from_iterable(elem.split(" ")
> >   for elem in source.split(",")))

You seriously find that clearer than re.split('[ ,]') above?
I have no further comment. :-)

> > It's weird looking, but delimiting text with two different
> > delimiters is weird.

Perhaps, but real-world input data is often very weird.
Try parsing a text "database" of a circa 1980 telephone
company phone directory sometime. :-)

> >[...]
>>> >>> - they are another language to learn, a very cryptic a terse
>>> >>> language;
>> >>
>> >> Chinese is cryptic too but there are a few billion people who
>> >> don't seem to be bothered by that.
> >
> > Chinese *would* be a problem if you proposed it as the solution
> > to a problem that could be solved by using a persons native
> > tongue instead.

My point was that "cryptic" is in large part an inverse function
of knowledge.  If I always go out of my way to avoid regexes, than
likely I will never become comfortable with them and they will
always seem cryptic.  To someone who uses them more often, they
will seem less cryptic.  They may never have the clarity of Python
but neither is Python code a very clear way to describe text
patterns.

As for needing to learn them (S D'A comment), shrug.  Programmers
are expected to learn new things all the time, many even do so
for fun.  REs (practical use that is) in the grand scheme of things
are not that hard.

They are I think a lot easier to learn than SQL, yet it is common
here to see recommendations to use sqlite rather than an ad-hoc
concoction of Python dicts.

> >[...]
>>> >>> - and thanks in part to Perl's over-reliance on them, there's
>>> >>> a tendency among many coders (especially those coming from
>>> >>> Perl) to abuse and/or misuse regexes; people react to that
>>> >>> misuse by treating any use of regexes with suspicion.
>> >>
>> >> So you claim.  I have seen more postings in here where
>> >> REs were not used when they would have simplified the code,
>> >> then I have seen regexes used when a string method or two
>> >> would have done the same thing.
> >
> > Can you find an example or invent one? I simply don't remember
> > such problems coming up, but I admit it's possible.

Sure, the response to the OP of this thread.



More information about the Python-list mailing list