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

2024-06-02 14:11:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道字符串的正文是什么。例如,字符串可以是
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或regex还是一些我从未听说过的模块/库?如果你有任何问题,请在下面发表评论。我希望最大数量的文本不被分隔\n。下面是我想提取的一些文本的一个例子:pastebin.com/CJsDYSLR我希望文本位于(和更多站点)和文档之间。我使用这个查找功能与家庭助理机器人。这够清楚吗?提前谢谢

-用户9311010


Tags: 字符串text文本isitsomethisbut
1条回答
网友
1楼 · 发布于 2024-06-02 14:11:28

你可以这样做吗

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

像这样:

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)

相关问题 更多 >