Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python使用搜索引擎查找文本文件中的文本_Python_Search Engine - Fatal编程技术网

python使用搜索引擎查找文本文件中的文本

python使用搜索引擎查找文本文件中的文本,python,search-engine,Python,Search Engine,我在一个目录中有很多文本文件。然后我会向用户询问一个关键字。如果用户输入例如:“hello” 然后,它必须搜索文本文件中所有目录的整个文本文件,然后搜索并返回文本文件的行,具有word hello的高优先级 例如: 输出: filename: abcd.txt line : this world is a good world saying hello 给我一些如何处理这些问题的想法 import subprocess output = subprocess.check_output(["/u

我在一个目录中有很多文本文件。然后我会向用户询问一个关键字。如果用户输入例如:“hello”
然后,它必须搜索文本文件中所有目录的整个文本文件,然后搜索并返回文本文件的行,具有word hello的高优先级

例如:

输出:

filename: abcd.txt
line : this world is a good world saying hello
给我一些如何处理这些问题的想法

import subprocess
output = subprocess.check_output(["/usr/bin/env", "grep", "-nHr", "hello", "."])
matches = (line.split(":", 2) for line in output.split("\n") if line != "")
for [file, line, text] in matches:
    ....
这将在当前目录或下面找到所有提到“hello”的地方
man grep
了解选项的详细信息。请注意,您需要引用任何特殊字符;如果你在寻找简单的词,这是没有必要的,但如果你在处理用户输入,你需要关心它


这将在当前目录或下面找到所有提到“hello”的地方
man grep
了解选项的详细信息。请注意,您需要引用任何特殊字符;如果您正在查找简单的单词,这是不必要的,但如果您正在处理用户输入,您需要关心它。

使用glob作为替代,您可以筛选特定的文件名、扩展名或目录中的所有文件

>>> from glob import glob
>>> key = 'hello'
>>> for file in glob("e:\data\*.txt"):
    with open(file,'r') as f:
        line_no = 0
        for lines in f:
            line_no+=1
            if key.lower() in lines.lower():
                print "Found in " + file + "(" + str(line_no) + "): " + lines.rstrip()

Found in e:\data\data1.txt(1): Hello how are you
Found in e:\data\data2.txt(4): Searching for hello
Found in e:\data\data2.txt(6): 3 hello

使用glob作为替代,您可以筛选特定的文件名、扩展名或目录中的所有文件

>>> from glob import glob
>>> key = 'hello'
>>> for file in glob("e:\data\*.txt"):
    with open(file,'r') as f:
        line_no = 0
        for lines in f:
            line_no+=1
            if key.lower() in lines.lower():
                print "Found in " + file + "(" + str(line_no) + "): " + lines.rstrip()

Found in e:\data\data1.txt(1): Hello how are you
Found in e:\data\data2.txt(4): Searching for hello
Found in e:\data\data2.txt(6): 3 hello

委派到
grep
,将比Python中的任何操作都要快。根据文件的数量、大小等,您可能需要查看Whoosh,这是一个纯文本编写的全文索引包Python@Amadan你能解释一下或者给我一些链接吗?@duhaime是的,我听说了,但是我没有看到一个与我的问题有关的网络例子。你能给我提供一些链接吗?@duhaime:这是你能以最快的速度获取非索引数据,只要你注意适当的regexp/shell转义(这是后面的痛点)。如果您有大量数据,您仍然可以使用
Popen
长距离(使用管道)而不是
check\u output
快捷方式来完成。也就是说,任何对数据进行索引而不是进行原始搜索的方法都会比我写的黑客更快委派到
grep
,将比Python中的任何操作都要快。根据文件的数量、大小等,您可能需要查看Whoosh,这是一个纯文本编写的全文索引包Python@Amadan你能解释一下或者给我一些链接吗?@duhaime是的,我听说了,但是我没有看到一个与我的问题有关的网络例子。你能给我提供一些链接吗?@duhaime:这是你能以最快的速度获取非索引数据,只要你注意适当的regexp/shell转义(这是后面的痛点)。如果您有大量数据,您仍然可以使用
Popen
长距离(使用管道)而不是
check\u output
快捷方式来完成。也就是说,任何对数据进行索引而不是进行原始搜索的方法都会比我写的黑客更快