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.

Was it helpful?

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', '.']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top