Reputation: 21
using Python, I'm trying to replace the special chars in the following text:
"The# dog.is.yelling$at!Me to"
to spaces so that afterwards I would get the list:
['The','dog','is','yelling','at','me']
How do I do that in one line?
Upvotes: 0
Views: 76
Reputation: 48347
You can use regular expressions:
>>> import re
>>> re.split("[#$!.\s]+", "The# dog.is.yelling$at!Me to" )
['The', 'dog', 'is', 'yelling', 'at', 'Me', 'to']
Or to split by any non alpha-numeric sequence of chars, as pointed @thg435:
>>> re.split("\W+", "The# dog.is.yelling$at!Me to" )
Upvotes: 2