使用python从文本中仅提取指定的值

时间:2018-07-16 22:06:12

标签: regex python-3.x spacy data-extraction

我有一个包含以下数据的文本文件。我必须提取所有包含签名的行

The document was signed on July 12

The document was signed by Charlie

This document was assigned to John

The document was preassigned to Amanda

预期输出:

The document was signed on July 12

The document was signed by Charlie

如果我正在使用:

for line in file:
    if "signed" in line:
        print (line)

正在打印所有行

1 个答案:

答案 0 :(得分:0)

使用单词边界\b可以轻松完成此操作。 \ bsigned将匹配已签名,但未分配。

See here

您可以使用re.search(line, ".*\bsigned.*")