[Python-ideas] This seems like a wart to me...

Adam Olsen rhamph at gmail.com
Fri Dec 12 01:18:24 CET 2008


On Thu, Dec 11, 2008 at 4:58 PM, Ron Adam <rrr at ronadam.com> wrote:
> There doesn't seem to be an obvious way to split on different characters.
>
>
> A new to python programmer might try:
>
>>>> '1 (123) 456-7890'.split(' ()-')
> ['1 (123) 456-7890']
>
> Expecting: ['1', '123', '456', '7890']
>
>
>>>> '1 (123) 456-7890'.split([' ', '(', ')', '-'])
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: expected a character buffer object

>>> re.split('[ ()-]', '1 (123) 456-7890')
['1', '', '123', '', '456', '7890']
>>> re.split('[ ()-]+', '1 (123) 456-7890')
['1', '123', '456', '7890']

str.split() handles the simplest, most common cases.  Let's not
clutter it up with a bad[1] impersonation of regex.


[1] And if you thought regex was ugly enough to begin with...

-- 
Adam Olsen, aka Rhamphoryncus



More information about the Python-ideas mailing list