how to split sentences with multiple splitting parameters and including split elements [duplicate]

StackOverflow https://stackoverflow.com/questions/23631949

Вопрос

I have a text suppose 'this is my a,b,c,and d.'

I want a list corresponding to the text as

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

The present split() method eliminated element by which it is splitting and it does not support multiple splitting parameters.

I want list from a string spliced with respect to special characters including the special characters in the list. What is the simplest way to achieve it.

Это было полезно?

Решение

You can find all the consecutive word characters and the non-word characters. That should give you the following result.

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', '.']
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top