Split text using Python [duplicate]
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
精彩评论