Eagle Eagle - 3 months ago 11
Python Question

python split a string with at least 2 whitespaces

I would like to split a string only where there are at least two or more whitespaces.

For example

str = '10DEUTSCH GGS Neue Heide 25-27 Wahn-Heide -1 -1'
print str.split()


Results:

['10DEUTSCH', 'GGS', 'Neue', 'Heide', '25-27', 'Wahn-Heide', '-1', '-1']


and i would like it to see like this:

['10DEUTSCH', 'GGS Neue Heide 25-27', 'Wahn-Heide', '-1', '-1']

Answer
In [4]: import re    
In [5]: text = '10DEUTSCH        GGS Neue Heide 25-27     Wahn-Heide   -1      -1'
In [7]: re.split(r'\s{2,}', text)
Out[7]: ['10DEUTSCH', 'GGS Neue Heide 25-27', 'Wahn-Heide', '-1', '-1']