Strip Punctuation From String in Python

2019-09-18 12:22发布

I`m working with documents, and I need to have the words isolated without punctuation. I know how to use string.split(" ") to make each word just the letters, but the punctuation baffles me.

1条回答
Luminary・发光体
2楼-- · 2019-09-18 13:10

this is an example using regex, and the result is ['this', 'is', 'a', 'string', 'with', 'punctuation']

s = " ,this ?is a string! with punctuation. "
import re
pattern = re.compile('\w+')
result = pattern.findall(s)
print(result)
查看更多
登录 后发表回答