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

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

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

I would like it to look like this:

['10DEUTSCH', 'GGS Neue Heide 25-27', 'Wahn-Heide', '-1', '-1']
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...