Python:在关键字前后抓取文本

标签 python string python-2.7 substring

keywords = ("banana", "apple", "orange", ...)
before = 50
after = 100
TEXT = "a big text string,  i.e., a page of a book"

for k in keywords:
    if k in TEXT:
        #cut = portion of text starting 'beforeText' chars before occurrence of 'k' and ending 'afterText' chars after occurrence of 'k'
        #finalcut = 'cut' with first and last WORDS trimmed to assure starting words are not cut in the middle

伙计们,你能帮我编写上面例子中的 cutfinalcut 字符串变量吗?

考虑到我要处理大文本、大量页面和可能超过 20 个要搜索的关键字,最有效的解决方案是什么?

最佳答案

您可以使用 re.finditer 查找字符串中的所有匹配项.每个匹配对象都有一个 start()方法可以用来计算字符串中的位置。您也不需要检查键是否在字符串中,因为 finditer 会返回一个空迭代器:

keywords = ("banana", "apple", "orange", ...)
before = 50
after = 100
TEXT = "a big text string,  i.e., a page of a book"

for k in keywords:
    for match in re.finditer(k, TEXT):
        position = match.start()
        cut = TEXT[max(position - before, 0):position + after] # max is needed because that index must not be negative
        trimmed_match = re.match("\w*?\W+(.*)\W+\w*", cut, re.MULTILINE)
        finalcut = trimmed_match.group(1)

正则表达式修剪所有内容,包括第一个非单词字符序列和最后一个非单词字符序列(我添加了 re.MULTILINE 以防换行在你的文字中)

关于Python:在关键字前后抓取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25277305/

相关文章:

python - 从用户输入中使用 pycountry 获取国家代码

python - 使用 seaborn/matplotlib boxplot 时的刻度频率

python - 按下按钮时执行功能 (tkinter)

python - 为什么邻接矩阵的特征值实际上是Textrank中的句子分数

c++ - 带排除项的字符串搜索

python - python 中的一切都是对象,但为什么不将关键字作为对象呢?

python - 带日期的 matplotlib 条形图

javascript - 按字符位置查找行

python - 函数返回元组而不是字符串

python - mysql for python 2. 7 说找不到 Python v2.7