本站消息

站长简介/公众号


站长简介:逗比程序员,理工宅男,前每日优鲜python全栈开发工程师,利用周末时间开发出本站,欢迎关注我的微信公众号:幽默盒子,一个专注于搞笑,分享快乐的公众号

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

写入索引时发出锁定语句

发布于2021-07-30 15:14     阅读(719)     评论(0)     点赞(21)     收藏(4)



我试图建立一个议会记录的索引,我已经按页码分割并转换为 txt。我正在使用最新版本的 whoosh 来构建它,但它给了我锁定错误。这是代码:

import os
from whoosh import index
from whoosh.index import create_in
from whoosh.fields import Schema, TEXT, ID
import sys

def createSearchableData(root):   

    '''
    Schema definition: title(name of file), path(as ID), content(indexed
    but not stored),textdata (stored text content)
    '''
    schema = Schema(title=TEXT(stored=True),path=ID(stored=True),\
              content=TEXT,textdata=TEXT(stored=True))
    if not os.path.exists("indexdir"):
        os.mkdir("indexdir")

    # Creating a index writer to add document as per schema
    ix = index.create_in("indexdir",schema)
    writer = ix.writer()

    filepaths = [os.path.join(root,i) for i in os.listdir(root)]
    for path in filepaths:
        fp = open(path,'rb')
        print(path)
        text = fp.read()
        writer.add_document(title=path.split("\\")[1], path=path,\
          content=text,textdata=text)
        fp.close()
    writer.commit()

root = "C:\\Users\\vitop\\OneDrive\\Desktop\\Final Project\\Test\\Splitted\\Txt"
createSearchableData(root)

这是指出的错误:

LockError                                 Traceback (most recent call last)
<ipython-input-4-e8b4a33a2859> in <module>
     31 
     32 root = "C:\\Users\\vitop\\OneDrive\\Desktop\\Final Project\\Test\\Splitted\\Txt"
---> 33 createSearchableData(root)

<ipython-input-4-e8b4a33a2859> in createSearchableData(root)
     18     # Creating a index writer to add document as per schema
     19     ix = index.create_in("indexdir",schema)
---> 20     writer = ix.writer()
     21 
     22     filepaths = [os.path.join(root,i) for i in os.listdir(root)]

~\Anaconda3\lib\site-packages\whoosh\index.py in writer(self, procs, **kwargs)
    462         else:
    463             from whoosh.writing import SegmentWriter
--> 464             return SegmentWriter(self, **kwargs)
    465 
    466     def lock(self, name):

~\Anaconda3\lib\site-packages\whoosh\writing.py in __init__(self, ix, poolclass, timeout, delay, _lk, limitmb, docbase, codec, compound, **kwargs)
    513             if not try_for(self.writelock.acquire, timeout=timeout,
    514                            delay=delay):
--> 515                 raise LockError
    516 
    517         if codec is None:

LockError: 

该代码创建了文件夹并设置了索引文件。我尝试使用带有 Whoosh文档的解决方法建议,但它不起作用。


解决方案


修复了摆脱二进制选项并将 txt 文件编码为“utf-8”的问题。

for path in filepaths:
    fp = open(path,'r', encoding = "utf-8") 
    print(path)
    text = fp.read()
    writer.add_document(title=path.split("\\")[1], path=path,
    content=text,textdata=text)
    fp.close()
writer.commit()





所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1006900/5152388a20fe137136d3/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)