python - 在python中搜索文档中的关键字

标签 python keyword

我正在尝试编写一个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/

相关文章:

operator-keyword - C# 自定义迭代器实现

c - C 中的 "HANDLE"关键字

python - 自定义 DRF 列表函数不返回某些字典键/值对

python - Pandas - 将列值与字典中的值进行比较

Python:list() 函数搞乱了 map()

铿锵工具。键入名称不带关键字

sql - SQLite3 中的 REFERENCES 关键字

python - 如何阻止 Scrapy CrawlSpider 跟踪超出所需的 URL?

python - 在 Django 模型中,外键字段如何知道与其他模型中的哪个字段匹配?

JavaScript 停止 "this"关键字在函数调用时发生变化?