On Fri, 26 Jan 2007 11:26:46 -0700, Bob Greschke <>
declaimed the following in comp.lang.python:
>
> That's fancy enough.
I didn't know you could do [""]*n. I never
> thought about it before.
>
My first thought was getting it from the other side...
>>> def nsplit(st, sp, n):
.... return (st + (sp*n)).split(sp)[:n]
....
>>> nsplit("this;is;a;sample", ";", 10)
['this', 'is', 'a', 'sample', '', '', '', '', '', '']
To the string to be split, append enough separators to ensure the
desired number of fields, perform the split, and return the desired
number of resultant parts.
Of course, if the string is longer than "n", it will only return the
leftmost "n" parts.
>>> nsplit("this;is;a;sample", ";", 4)
['this', 'is', 'a', 'sample']
>>> nsplit("this;is;a;sample", ";", 3)
['this', 'is', 'a']
>>>
--
Wulfraed Dennis Lee Bieber KD6MOG
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff:
web-)
HTTP://www.bestiaria.com/