我正在尝试编写一个Python脚本,以便它可以在文档中搜索关键字,并检索关键字所在的整个句子。根据我的研究,我发现 acora 可以使用,但我仍然发现它不成功。
最佳答案
>>> text = """Hello, this is the first sentence. This is the second.
And this may or may not be the third. Am I right? No? lol..."""
>>> import re
>>> s = re.split(r'[.?!:]+', text)
>>> def search(word, sentences):
return [i for i in sentences if re.search(r'\b%s\b' % word, i)]
>>> search('is', s)
['Hello, this is the first sentence', ' This is the second']
关于python - 在python中搜索文档中的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6530731/