我有700多个小的,本地存储的文本文件(不超过2Kb大)。每个文件位于一个文件夹中,如:
2012//05//18.txt2013//09/22.txt2014//11//29.txt(每个文件代表一天)。
内容包含每日的“事件”信息。我感兴趣的是能够找到包含特定单词的文件。
发布于 2014-06-01 00:00:30
前言:我从来没有用过哇哦。我刚看了快速启动。
似乎要完成这一任务是非常微不足道的。你要做的工作的关键是在这里:
writer.add_document(title=u"First document", path=u"/a", content=u"This is the first document we've added!")这就是真正创建索引的地方。本质上,您需要做的似乎是使用类似os.walk的东西来爬行您的根目录,并填写数据,并为它找到的每个文件填写writer.add_document参数。
for root, dirs, files in os.walk("/your/path/to/logs"):
for file in files:
filepath = os.path.join(root, file)
with open(filepath, 'r') as content_file:
file_contents = content_file.read()
writer.add_document(title=file, content=the_contents, path=filepath)至少这是个起点。在此之后,使用Whoosh提供的Searcher运行索引文件似乎非常简单。
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse("somesearchstring")
results = searcher.search(query)
print(results)就像我说的,我从来没有用过,但是如果我是你,这就是我会做的。
https://stackoverflow.com/questions/23974979
复制相似问题