Noober
Noober

Reputation: 1626

Split word in Python

I am trying to use re.split in python. I want to remove all these characters like " , ; < > { } [ ] / \ ? ! .I am trying to do something like this-

re.split("[, \_!?,;:-]+", word)

How can I add characters like " ( ) or < > ' so that they can also be removed?

Edit

re.split('\W+',word)

This works fine but it is not removing underscore symbol. How can I also remove underscore?

Upvotes: 0

Views: 122

Answers (2)

pwilmot
pwilmot

Reputation: 596

checkout the str.translate function for example in python 2.6+

line = line.translate(None, " ?.!/;:")

or in python 3+

line = line.translate(" ?.!/;:")

see Remove specific characters from a string in python

Upvotes: 2

Remi Guan
Remi Guan

Reputation: 22282

Try:

re.split('\W+|\_', word)

Also just remove them:

re.sub('\W+|\_', '', word)

Take a look at the document for more details.

Upvotes: 2

Related Questions