Python如何提取符号
符号在文本处理中占有重要的地位,它们可以表示数学运算、标点符号、注释、分割等等。在Python中,我们可以利用一些库和方法来提取符号。
正则表达式
正则表达式是一个强大的文本匹配工具,使用正则表达式可以快速提取符号。
import re
text = "Hello world! This is an example sentence, with punctuation."
# 提取所有标点符号
punctuation = re.findall(r'[^\w\s]', text)
print(punctuation)
# Output: ['!', ',', '.']
<