Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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']

I would like it to look like this:

['10DEUTSCH', 'GGS Neue Heide 25-27', 'Wahn-Heide', '-1', '-1']
question from:https://stackoverflow.com/questions/12866631/python-split-a-string-with-at-least-2-whitespaces

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
125 views
Welcome To Ask or Share your Answers For Others

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']

Update 2021+ answer.

str.split now accepts regular expressions to split on.

read more here

row = '10DEUTSCH        GGS Neue Heide 25-27     Wahn-Heide   -1      -1'
df = pd.DataFrame({'string' : row},index=[0])

print(df)
                                              string
0  10DEUTSCH        GGS Neue Heide 25-27     Wahn...

df1 = df['string'].str.split('s{2,}',expand=True)
print(df1)

           0                     1           2   3   4
0  10DEUTSCH  GGS Neue Heide 25-27  Wahn-Heide  -1  -1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...