user3045065
user3045065

Reputation: 21

replace special chars to space and create list

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

Answers (1)

alko
alko

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

Related Questions