在 Python 中从字符串中去除标点符号 [英] Strip Punctuation From String in Python
本文介绍了在 Python 中从字符串中去除标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理文档,我需要将单词分开而不带标点符号.我知道如何使用 string.split(" ") 使每个单词都只是字母,但标点符号让我感到困惑.
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.
推荐答案
这是一个使用正则表达式的例子,结果是['this', 'is', 'a', 'string', 'with', 'punctuation']
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)
这篇关于在 Python 中从字符串中去除标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文