Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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_Whoosh - Fatal编程技术网

Python 呼呼声无法成功搜索关键字

Python 呼呼声无法成功搜索关键字,python,whoosh,Python,Whoosh,1.我正在写一个非常简单的嗖嗖项目。首先,我读取一个txt文件,并使用read()方法获取txt文件中的所有内容。然后为该内容建立索引 2.以下是实施代码: 对于txt文件内容: #import functions from whoosh import whoosh from whoosh.index import create_in from whoosh.fields import * from whoosh.qparser import QueryParser schema = Sch

1.我正在写一个非常简单的嗖嗖项目。首先,我读取一个txt文件,并使用read()方法获取txt文件中的所有内容。然后为该内容建立索引

2.以下是实施代码:

对于txt文件内容:

#import functions from whoosh
import whoosh
from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser


schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
ix = create_in(".", schema)
writer = ix.writer()
i = 0
f = open("read.txt", "r")
print(f.read())
writer.add_document(title=u"document "+str(i), path=u".",content=f.read()) #python iterator i starting from 0
writer.commit(optimize=True)
searcher = ix.searcher()


parser = QueryParser("content", ix.schema)
stringquery = parser.parse("Hello")
results = searcher.search(stringquery)
print ("search 1 result:")
print (results)
for r in results:
    print (r)
Hello this is the test 
I hope you are doing well
I think you can do it without problem 
This is so cool without funciton
对于txt文件内容:

#import functions from whoosh
import whoosh
from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser


schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
ix = create_in(".", schema)
writer = ix.writer()
i = 0
f = open("read.txt", "r")
print(f.read())
writer.add_document(title=u"document "+str(i), path=u".",content=f.read()) #python iterator i starting from 0
writer.commit(optimize=True)
searcher = ix.searcher()


parser = QueryParser("content", ix.schema)
stringquery = parser.parse("Hello")
results = searcher.search(stringquery)
print ("search 1 result:")
print (results)
for r in results:
    print (r)
Hello this is the test 
I hope you are doing well
I think you can do it without problem 
This is so cool without funciton
“Hello”假设存储在索引中,但当我尝试搜索Hello时,它不会返回任何内容

search 1 result:
<Top 0 Results for Term('content', 'hello') runtime=7.878600001731684e-05>
搜索1结果:

第一次调用
f.read()
打印文件中的文本,下一次调用
f.read()
没有要读取的内容,也没有返回任何内容。存储文本

file_content = f.read()
print(file_content)
writer.add_document(title=u"document "+str(i), path=u".", content=file_content)
进一步证明,

$ cat test.txt
Hello this is the test
I hope you are doing well
I think you can do it without problem
This is so cool without funciton
$ ipython
Python 3.9.0 (default, Dec  2 2020, 10:34:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: f = open("test.txt", "r")

In [2]: print(f.read())
Hello this is the test
I hope you are doing well
I think you can do it without problem
This is so cool without funciton


In [3]: print(f.read())


In [4]: quit

你的意思是f.read()什么都不读,所以内容也将是空的吗?我是否应该更改您在注释中键入的代码?您当前调用
f.read()
两次。第一次读取文件内容。第二次什么也不读。