string.split

Fredrik Lundh fredrik at pythonware.com
Wed Sep 5 04:25:38 EDT 2001


Tom Harris wrote:
> How do I split on any or all occurrences of (for example)
> whitespace and a comma, without using regexes.

are you interested in results, or are you just trying to make
python fit your mental model?

if the former, this does what you want:

    L = re.split("[\s,]+", S)

or faster, in the current version:

    L = re.findall("[^\s,]+", S)

if you're going to do lot of splitting, create a splitter object:

    mysplit = re.compile("[^\s,]+").findall

    L = mysplit(S)

> I mean string.split() must be able to do it anyway to achieve
> the default behaviour.

split uses different algorithms depending on the second argument
(the unicode type has three different implementations)

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list