开发者

Split text using Python [duplicate]

This question already has a开发者_Go百科nswers here: Closed 11 years ago.

Possible Duplicate:

Python split string on regex

How do I split some text using Python's re module into two parts: the text before a special word cut and the rest of the text following it.


You can do it with re:

>>> import re
>>> re.split('cut', s, 1) # Split only once.

But in this case you can just use str.split:

>>> s.split('cut', 1) # Split only once.


Check this, might help you

>>> re.compile('[0-9]+').split("hel2l3o")
['hel', 'l', 'o']
>>> 

>>> re.compile('cut').split("hellocutworldcutpython")
['hello', 'world', 'python']

split about first cut 

>>> l=re.compile('cut').split("hellocutworldcutpython")
>>> print l[0], string.join([l[i] for i in range(1, len(l))], "")
hello worldpython
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜