如何在python中拆分下面的字符串如下?

时间:2016-05-05 16:24:36

标签: python regex string parsing split

字符串示例 - >预期产出

"    1     1    1-1 SOME MESSAGE HERE" --> ['1','1','1-1','SOME MESSAGE HERE']

"   71    91    1-1 SOME MESSAGE HERE" --> ['71','91','1-1','SOME MESSAGE HERE']

"  171   191    1-1 SOME MESSAGE HERE" --> ['171','191','1-1','SOME MESSAGE HERE']

" 1171  1191    1-1 SOME MESSAGE HERE" --> ['1171','1191','1-1','SOME MESSAGE HERE']

1 个答案:

答案 0 :(得分:2)

在Python 2.7中

x = "    1     1    1-1 SOME MESSAGE HERE"
y = x.split(None, 3)
print (y)  # ['1', '1', '1-1', 'SOME MESSAGE HERE']

在Python 3中

x = "    1     1    1-1 SOME MESSAGE HERE"
y = x.split(maxsplit=3)
print (y)  # ['1', '1', '1-1', 'SOME MESSAGE HERE']