0

我有一个 python 类,允许用户选择路径以显示文件,然后用户输入搜索的单词以将其与所有列出的文件匹配。

一旦用户点击搜索按钮,我需要系统

  1. 阅读列出的文件
  2. 通过仅显示匹配的文件来覆盖列表

系统显示此错误:

searchWord self.listWidgetPDFlist.addItems(listFiles)

builtins.TypeError:索引 0 的类型为“bool”,但应为“str”

对于这个任务,我写了 3 个函数

初始化函数

def __init__(self,PdfPreviewObj):
        #QWidget.__init__(self)
        self.PdfPreviewObj =PdfPreviewObj 
        self.setupUi(PdfPreviewObj)
        self.PdfPreviewObj.show()
        self.pushButtonOpenFolder.clicked.connect(self.setExistingDirectory)
        self.pushButtonSearch.clicked.connect(self.searchWord)

读取文件函数

def readFile(self, currentFile):
    try:
        currentFile = self.listWidgetPDFlist.currentItem().text()
        print(currentFile)

        with open(currentFile) as ctf:
            ctfRead = ctf.read()
            print(ctfRead)
            return(ctfRead)

    except Exception as e:
        print("the selected file is not readble because :  {0}".format(e))     

displayFilteredFile 函数

    def displayFilteredFiles(self, filteredFiles):
            try:

                filteredFiles = self.listWidgetPDFlist.items()
                print(filteredFiles)
                for  file in filteredFiles:
                    with open(file) as ctf:
                        ctfRead = ctf.read()
                        print(ctfRead)
                    return(ctfRead)

            except Exception as e:
                print("the selected file is not readble because :  {0}".format(e))     

searchedWord 函数

 def searchWord(self,filteredFiles): 

        listFiles = []
        self.textEdit_PDFpreview.clear()

        self.listWidgetPDFlist.clear()
        listFiles.append(filteredFiles)

       # here the system indicate the error 
        self.listWidgetPDFlist.addItems(listFiles)

        filesToSearchInside = self.displayFilteredFiles(listFiles)
        searchedSTR = self.lineEditSearch.text()
        RepX="<b><span style='color:white;mso-themecolor:background1;background:black;mso-highlight:black'>"+searchedSTR+'</span></b>'

        try:
            textList = filesToSearchInside.split('\n')

            # utility that gives an empty list for each key by default
            d = defaultdict(list)   

            '''
            looping over the selected text and search for the required word in the search box 
            store the word and the number of occurence in a dictionnary  to used for later.
            '''
            for counter, myLine in enumerate(textList):
                thematch=re.sub(searchedSTR,RepX,myLine)
                matches = re.findall(searchedSTR, myLine, re.MULTILINE | re.IGNORECASE)
                if len(matches) > 0:

                    # add one record for the match (add one because line numbers start with 1)
                    d[matches[0]].append(counter + 1)


                    self.textEdit_PDFpreview.insertHtml(str(thematch))
                    #self.textEdit_PDFpreview.insertHtml(result)



                # print out 
            for match, positions in d.items():
                print('{} exists {} times'.format(match, len(positions)))
                for p in positions:
                    print("on line {}".format(p))

        except Exception as e:
                print(e)        
4

0 回答 0