2013-07-27 26 views
0

我作为Hi, my name is Will, And i am from Canada. I have 2 pets. One is a dog and the other is a Zebra. Ahoi! Thanks.分离使用预定义字符的字符串

我想从.和拆分这句话文本“!”,我怎么能做到这一点。我也想知道从哪个角色分句子。

例如结果应该是:

实施例1:

Hi, my name is Will, And i am from Canada || The sentence was split with .

实施例2:

Ahoi! || The sentence was split with !

我怎样才能做到这一点?我的工作到目前为止:

print (text.split('.')) - 这只打破与.的句子,我无法确定它用于分裂的字符。

+0

是要知道哪些字是用来分割或如何分割句子的问题? –

回答

5

可以使用re.split()

re.split('[.!]', text) 

此分割上的任何字符在[...]字符类:

>>> import re 
>>> text = 'Hi, my name is Will, And i am from Canada. I have 2 pets. One is a dog and the other is a Zebra. Ahoi! Thanks.' 
>>> re.split('[.!]', text) 
['Hi, my name is Will, And i am from Canada', ' I have 2 pets', ' One is a dog and the other is a Zebra', ' Ahoi', ' Thanks', ''] 

可以组分割表达以包括在单独的列表中的元素的字符在输出中:

>>> re.split('([.!])', text) 
['Hi, my name is Will, And i am from Canada', '.', ' I have 2 pets', '.', ' One is a dog and the other is a Zebra', '.', ' Ahoi', '!', ' Thanks', '.', ''] 

以保持附着在句子中的标点符号,使用re.findall()代替:

>>> re.findall('[^.!]+?[.!]', text) 
['Hi, my name is Will, And i am from Canada.', ' I have 2 pets.', ' One is a dog and the other is a Zebra.', ' Ahoi!', ' Thanks.'] 
0
>>> sp=re.split('(\.)|(!)','aaa.bbb!ccc!ddd.eee') 
>>> sp 
['aaa', '.', None, 'bbb', None, '!', 'ccc', None, '!', 'ddd', '.', None, 'eee'] 
>>> sp[::3] # the result 
['aaa', 'bbb', 'ccc', 'ddd', 'eee'] 
>>> sp[1::3] # where matches `.` 
['.', None, None, '.'] 
>>> sp[2::3] # where matches `!` 
[None, '!', '!', None] 
+0

如何从输出中删除'None' –