【问题标题】:Python searching for exact word/phrase within a text filePython 在文本文件中搜索准确的单词/短语
【发布时间】:2014-12-03 22:56:23
【问题描述】:

目前,我正在尝试在文本文件中搜索确切的单词/短语。我正在使用 Python 3.4

这是我目前的代码。

import re

def main():
    fileName = input("Please input the file name").lower()
    term = input("Please enter the search term").lower()

    fileName = fileName + ".txt"

    regex_search(fileName, term)

def regex_search(file,term):
    source = open(file, 'r')
    destination = open("new.txt", 'w')
    lines = []
    for line in source:
        if re.search(term, line):
            lines.append(line)

    for line in lines:
        destination.write(line)
    source.close()
    destination.close()
'''
def search(file, term): #This function doesn't work
    source = open(file, 'r')
    destination = open("new.txt", 'w')
    lines = [line for line in source if term in line.split()]

    for line in lines:
        destination.write(line)
    source.close()
    destination.close()'''
main()

在我的函数 regex_search 中,我使用正则表达式来搜索特定的字符串。但是,我不知道如何搜索特定的短语。

在第二个函数搜索中,我将行拆分为一个列表并在其中搜索单词。但是,这将无法搜索特定的短语,因为我在 ['the','dog','walked'] 中搜索 ["dog walk"] 不会返回正确的行。

【问题讨论】:

  • 如果您搜索“foo”并且文本中有“foobar”,这是否被视为匹配?如果您搜索“foo bar”,并且一行以“foo”结尾,下一行以“bar”开头,这是否被视为匹配?
  • 您能否提供输入文件(或其内容)的示例以及感兴趣的短语?
  • @Brian Oakley 两者都没有

标签: python


【解决方案1】:

edit: 考虑到你不想匹配部分单词('foo' 不应该匹配'foobar'),你需要在数据流中向前看。代码有点尴尬,所以我认为正则表达式(你当前的 regex_search 有修复)是要走的路:

def regex_search(filename, term):
    searcher = re.compile(term + r'([^\w-]|$)').search
    with open(file, 'r') as source, open("new.txt", 'w') as destination:
        for line in source:
            if searcher(line):
                destination.write(line)

【讨论】:

  • 那么在这种情况下,当我搜索 no 并且该行没有时会发生什么?它不会用 not 而不是 no 返回该行吗?
  • 'no' 将匹配带有 'not' 的行 - 与您的 regex_search 示例相同。如果这不是您想要的,请告诉我们。
  • 我正在寻找 no 只匹配 no。与短语相同。
猜你喜欢
  • 2020-03-22
  • 2015-08-28
  • 2014-11-08
  • 1970-01-01
  • 2018-09-27
  • 2023-03-28
  • 2013-09-17
  • 2014-02-18
  • 1970-01-01
相关资源
最近更新 更多