views:

259

answers:

4

Hi,

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.

+4  A: 

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 [].

Mark Byers
+4  A: 
>>> import re
>>> a="test                            result"
>>> re.split(" +",a)
['test', 'result']

>>> a.split()
['test', 'result']
ghostdog74
Cool. Might help with other, none-whitespace delimiters.
Adam Matan
re.split('\W+',mystring) is more equivalent string.split(None).
Gregg Lind
+1  A: 

Any problem with simple a.split()?

S.Mark
Apparently, None.
Adam Matan
+3  A: 

Just do not give any delimeter?

>>> a="test                            result"
>>> a.split()
['test', 'result']
Kimvais
As for why this works: a.split(None) is a special case, which in Python means "split on one or more whitespace chars". re.split() is the general case solution.
Gregg Lind