在python中查找字符串中的文本主体

时间:2018-03-29 01:50:47

标签: python text

我想知道字符串中的主体是什么。例如,字符串可以是
This is not so important neither is this But this is. This is some filler text: QWERTY ASDFGH ZXCVBN It could be on multiple lines. Or just all-together This is also just filler text

我想要

But this is. This is some filler text: QWERTY ASDFGH ZXCVBN It could be on multiple lines. Or just all-together

有没有简单的方法可以做到这一点?我应该使用NLTK或正则表达式还是以前从未听说过的模块/库?如果您有任何疑问,请在下面留言。我希望最大数量的文本不用\ n分隔。这是我想要提取的一些文本的示例:pastebin.com/CJsDYSLR我想要介于两个(和更多站点)和文档之间的文本。我正在使用它来获得一个带有家庭助理机器人的查找功能。这够清楚了吗?提前谢谢,

-user9311010

1 个答案:

答案 0 :(得分:1)

你可以这样做吗?

  • 将文字/字符串存储在变量
  • 将文本的重要部分存储在另一个变量
  • 检查重要部分是否在文本变量

像这样:

text = 
"""
This is many differant lines of text
[String-we-are-searching-for]
that are actually not that useful to us
we are looking for a specific string
"""

text_body = "[String-we-are-searching-for]"

if text_body in text:
    print(text_body)