Adam Matan
Adam Matan

Reputation: 136191

How can I split by 1 or more occurrences of a delimiter in Python?

I have a formatted string from a log file, which looks like:

>>> a="test                            result"

That is, the test and the result are split by some spaces - it was probably created using formatted string which gave test some constant spacing.

Simple splitting won't do the trick:

>>> a.split(" ")
['test', '', '', '', ... '', '', '', '', '', '', '', '', '', '', '', 'result']

split(DELIMITER, COUNT) cleared some unnecessary values:

>>> a.split(" ",1)
['test', '                           result']

This helped - but of course, I really need:

['test', 'result']

I can use split() followed by map + strip(), but I wondered if there is a more Pythonic way to do it.

Thanks,

Adam

UPDATE: Such a simple solution! Thank you all.

Upvotes: 56

Views: 72982

Answers (6)

anshu kumar
anshu kumar

Reputation: 747

Just adding one more way, more useful in cases where delimiter is different from space, and s.split() will not work.

like str = "Python,is,,more,,,,,flexible".

In [27]: s = "Python,is,,more,,,,,flexible"

In [28]: str_list = list(filter(lambda x: len(x) > 0, s.split(",")))

In [29]: str_list
Out[29]: ['Python', 'is', 'more', 'flexible']

Upvotes: 1

theferrit32
theferrit32

Reputation: 241

If you want to split by 1 or more occurrences of a delimiter and don't want to just count on the default split() with no parameters happening to match your use case, you can use regex to match the delimiter. The following will use one or more occurrences of . as the delimiter:

s = 'a.b....c......d.ef...g'
sp = re.compile('\.+').split(s)
print(sp)

which gives:

['a', 'b', 'c', 'd', 'ef', 'g']

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838226

Just this should work:

a.split()

Example:

>>> 'a      b'.split(' ')
['a', '', '', '', '', '', 'b']
>>> 'a      b'.split()
['a', 'b']

From the documentation:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

Upvotes: 24

Kimvais
Kimvais

Reputation: 39548

Just do not give any delimeter?

>>> a="test                            result"
>>> a.split()
['test', 'result']

Upvotes: 94

YOU
YOU

Reputation: 123831

Any problem with simple a.split()?

Upvotes: 4

ghostdog74
ghostdog74

Reputation: 342373

>>> import re
>>> a="test                            result"
>>> re.split(" +",a)
['test', 'result']

>>> a.split()
['test', 'result']

Upvotes: 52

Related Questions