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

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

Question

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.

Était-ce utile?

La solution

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', '.']
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top