3

I've been looking around here, but I didn't find anything that was close to my problem. I'm using Python3. I want to split a string at every whitespace and at commas. Here is what I got now, but I am getting some weird output: (Don't worry, the sentence is translated from German)

    import re
    sentence = "We eat, Granny" 
    split = re.split(r'(\s|\,)', sentence.strip())
    print (split)

    >>>['We', ' ', 'eat', ',', '', ' ', 'Granny']

What I actually want to have is:

    >>>['We', ' ', 'eat', ',', ' ', 'Granny']
n-anselm
  • 77
  • 1
  • 5

3 Answers3

3

I'd go for findall instead of split and just match all the desired contents, like

import re
sentence = "We eat, Granny" 
print(re.findall(r'\s|,|[^,\s]+', sentence))
Sebastian Proske
  • 8,255
  • 2
  • 28
  • 37
0

Alternate way:

import re
sentence = "We eat, Granny"
split = [a for a in re.split(r'(\s|\,)', sentence.strip()) if a]

Output:

['We', ' ', 'eat', ',', ' ', 'Granny']

Works with both python 2.7 and 3enter image description here

Vaulstein
  • 20,055
  • 8
  • 52
  • 73
0

This should work for you:

 import re
 sentence = "We eat, Granny" 
 split = list(filter(None, re.split(r'(\s|\,)', sentence.strip())))
 print (split)
zipa
  • 27,316
  • 6
  • 40
  • 58