Strip Punctuation From String in Python

无人久伴 提交于 2019-12-13 07:02:19

问题


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:


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)


来源:https://stackoverflow.com/questions/37063500/strip-punctuation-from-string-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!