2014-05-13 52 views
0

我有一個文本假設 'this is my a,b,c,and d.'具有多重分割參數如何拆分句子和包括分割構件

我想對應的文本

['this',' ','is',' ',my,' ','a',',','b',',','c',' ','and',' ','d','.']列表。

現在的split()方法消除了它正在拆分的元素,並且它不支持多個拆分參數。

我想要從與特殊字符(包括列表中的特殊字符)拼接的字符串列表。什麼是最簡單的方法來實現它。

+0

http://stackoverflow.com/questions/7866128/python-split-without-removing-the-delimiter – sshashank124

回答

2

您可以找到所有連續的單詞字符和非單詞字符。這應該會給你以下結果。

data = 'this is my a,b,c,and d.' 
import re 
print re.findall(r"\w+|\W+", data) 
# ['this', ' ', 'is', ' ', 'my', ' ', 'a', ',', 'b', ',', 'c', ',', 'and', ' ', 'd', '.']